c# - joining two tables in entity framework -
i'm trying join 2 tables in entity framework , value 1 of them query on third table query i'm using
var fav = favs in db.favorites join pins in db.pins on new { favs.user_id, favs.pin_id } equals new { userid, pins.pin_id } res r in res select new { favs.pin_id, r.type_id };
but gives me syntax error in the type of 1 of expressions in join clause incorrect. type inference failed in call 'groupjoin' have searched error , find people make sure properties in equals clause same type, , yes of type non nullable int
when doing linq join, types on either side of equals must same, in query have user_id vs. userid.
the fix simply:
var fav = favs in db.favorites join pins in db.pins on new { favs.user_id, favs.pin_id } equals // use explicit naming first property gets name user_id not userid new { user_id = userid, pins.pin_id } res r in res select new { favs.pin_id, r.type_id };
it's bit easier see why necessary if work fluent syntax groupjoin (what you're doing here due "into" clause; regular join similar).
the signature is:
public static iqueryable<tresult> groupjoin<touter, tinner, tkey, tresult>( iqueryable<touter> outer, ienumerable<tinner> inner, expression<func<touter, tkey>> outerkeyselector, expression<func<tinner, tkey>> innerkeyselector, expression<func<touter, ienumerable<tinner>, tresult>> resultselector )
note outerkeyselector , innerkeyselector must return same type tkey (the join done matching these keys).
to write original join in fluent style, you'd have:
var fav = db.favorites.groupjoin( inner: inner, // return types of selectors don't match, compiler can't // infer type tkey! outerkeyselector: favs => new { favs.user_id, favs.pin_id }, innerkeyselector: pins => new { userid, pins.pin_id }, resultselector: (favs, res) => res.select(r => new { favs.pin_id, r.type_id }) ) .selectmany(res => res);
Comments
Post a Comment