c# - Entity Framework list of objects contains list of object lambda -
following scenario:
public class user{ public virtual icollection<mediaitem> mediaitems { get; set; } } public enum emediaitemgenre { [display(name = "pop")] pop = 0, [display(name = "other")] other = 11 } public class mediaitem { public virtual icollection<mediaitemgenre> genres { get; set; } } public class mediaitemgenre { [key] public int32 id { get; set; } public emediaitemgenre genre { get; set; } public int32 mediaitemid { get; set; } public virtual mediaitem mediaitem { get; set; } }
now following: have mediaitem
, find mediaitems
share same genre
.
i did way:
list<mediaitem> litems = ltcontext.mediaitems.where(x => x.genres.any(y => pgenres.contains(y))).tolist();
but error
only primitive types or enumeration types supported in context.
the problem trying compare complex types in database list of complex types in memory, not possible. suggest doing converting pgenres
list of int
using projection:
list<int> pgenresid = pgenres.select(p => p.id).tolist();
then can use query:
list<mediaitem> litems = ltcontext.mediaitems .where(x => x.genres.select(g => g.id).any(y => pgenresid.contains(y))).tolist();
now linq-to-entities can convert query putting int
's of id
's in queries.
Comments
Post a Comment