c# - Concatenating three lists into one with LINQ throws an exception -
ok, must doing dumb, shouldn't work? have following 3 lists:
var commonviews = (from v in context.tpm_views v.viewid < 0 select v); // iqueryable<tpm_views> var ownedviews = (from v in context.tpm_views v.ownerid == userid && v.viewid > 0 select v); // iqueryable<tpm_views> var sharedviews = (from v in context.tpm_user.include("tpm_views2") v.userid == userid select v).first().tpm_views2; // entitycollection<tpm_views> each list has proper values , count. can return 1 of these lists:
return commonviews.tolist(); and can return two of these lists:
return commonviews.concat(ownedviews).tolist(); however, when try return three:
return commonviews.concat(ownedviews).concat(sharedviews).tolist(); i exception:
unable create constant value of type 'entity.tpm_views'. primitive types or enumeration types supported in context.
what doing wrong? 3 values indeed enumerable. mostly, i'm asking question because it's best possible way guarantee i'll notice problem 30 seconds after posting.
update:
i'm 93% sure problem here:
var sharedviews = (from v in context.tpm_user.include("tpm_views2") v.userid == userid select v).first().tpm_views2; this looks enumerable list of tpm_views object, , can call tolist() on , correct data, doesn't play other lists.
update 2:
this works. points person can tell me why!
commonviews.tolist().concat(ownedviews.tolist()).concat(sharedviews.tolist()).tolist();
the problem concat() on ef iqueryable<t> turn entire concatenation single query.
when call .concat(sharedviews), you're passing scalar (pre-loaded) collection of nested entity class.
ef doesn't know how convert query, complains.
you can make faster calling asenumerable() instead of tolist().
Comments
Post a Comment