entity framework - Type arguments error in LINQ extension method -
i wrote linq extension join 2 tables
public static ienumerable<objectdetail<tleft, string>> todetail<tleft, tright, tkey>(this ienumerable<tleft> left, ienumerable<tright> right, func<tleft, tkey> leftproperty, func<tright, tkey> rightproperty, func<tright, tkey> keyproperty, func<tright, tkey> valueproperty, string keyselector) { return (from l in left join r in right on leftproperty(l) equals rightproperty(r) 1 select new { l, 1 }).asenumerable().select(q => new objectdetail<tleft, string> { item = q.l, meta = q.one.where(m => keyselector.contains(keyproperty(m).tostring())).todictionary(m => keyproperty(m).tostring(), m => valueproperty(m).tostring()) }); }
the compiler understand extension when try write
var result = left.todetail(right, l => l.id, r => r.id, r => r.key, r => r.value, "key");
i got following error:
the type arguments method .todetail(system.collections.generic.ienumerable, system.collections.generic.ienumerable, system.func, system.func, system.func, system.func, string)' cannot inferred usage. try specifying type arguments explicitly
then tried remove keyproperty , valueproperty parameters extension, run without error. don't know why, maybe compiler confused , cannot determine parameter same type?
any helps appreciated!
in function declaration, use same function type rightproperty
, keyproperty
, valueproperty
arguments, both of these functions should have type func<tright, tkey>
.
this means when call function, type of r.id
, r.key
, r.value
must same (so c# compiler can infer tkey
type parameter these two). guess in example, @ least 1 of these 3 different (probably id
).
(aside, since you're converting result of valueselector
, keyselector
string using tostring
method, there no reason why need use generic types - function returning object
should trick).
Comments
Post a Comment