c# - Filter Sub-Collection of Collection -
c# - Filter Sub-Collection of Collection -
i have next classes:
class client { public int idcustomer {get; set} public string name {get; set;} public virtual icollection<expertise> expertises {get; set;} } class expertise { public int idexpertise {get; set;} public string description {get; set;} public virtual icollection<subexpertise> subexpertises {get; set;} } class subexpertise { public int idsubexpertise { get; set;} public int idexpertise {get; set;} public string description {get; set;} public virtual expertise expertise {get; set;} }
how can filter subexpertise using iqueryable, , maintain filter condition? want filter subexpertise, keeping possibilty add together more filters in query
example: wanna customers have subexpertise 'x' or 'y'. maintain in mind subexpertise sub-collection of collection (expertise). , can have new filters after this.
i think you're after method build expressions dynamically. utilize predicate builder job. favorite this one.
using little gem can this:
var pred = predicatebuilder.create<subexpertise>(s => s.description == "x"); pred = pred.or(s => s.description == "y"); var customers = db.customers .where(c => c.expertises .any(e => e.subexpertises.asqueryable() .any(pred)));
e.subexpertises
must cast iqueryable
because it's compile-time type icollection
, , extension method any
fits icollection
doesn't take expression
func
, doesn't compile.
c# linq entity-framework
Comments
Post a Comment