c# - Lambda expression tree for related entities -
looking create kind of generic query builder have related entities via navigation properties. want able like:
var results = entitya in context.entityas entitya.navigationpropertyb.propertyz = value1 grouping entitya entitya.navigationpropertyc.propertyy g select new { propertyy = g.key, value = g.operatord(x=>x.propertyx)}
where entitya, related properties , operator can provided on fly. thinking of getting collection of properties given entity type , using them build lambda look tree of dynamic query.
a) work? sensible?
b) there easy way of getting single collection of properties i'd want query on, including properties on related entities n number of relationships away entitya?
c) query tree including related entities passed in @ runtime?
well, have done similar.
i've used linq expressions (system.linq.expressions namespace), , i've created little parser also.
it took me time extremely happy it, it's beating heart whole application, yeah, works, , sensible.
to reply b): whatever can write in code in vs editor, can create (express) via linq expressions, because that's compiler anyway does. methods easier where() or count(), harder join() or groupby(), @ end of day, can done.
c) no matter whether write query statically, or build dynamically @ runtime, look tree same, gets evaluated when materializing.
here's example:
string query = "db.documents.count() != 0 ? db.documents.where(d => d.no == 176).items.select(a => a.debit).sum() : 0"; var result = exprbuilder.doexpression(new mydbcontext(), query);
so basically, it's not easy, may not worth time (depends how much need it), can done , can come handy.
c# linq entity-framework lambda navigation-properties
No comments:
Post a Comment