开发者

Dynamic Data Querying using Entity Framework (EF)

I would like to hear some feedback on a scenario that I am trying to implement. I have currently implemented this scenario using Criteria API for NHibernate and was wondering if there was anything similar that was implemented for Entity Framework 4.1.

I have a need where the end user can select filtering criteria from the UI and thus build a query that can contain complex AND/OR criteria.

For e.g.: The user can say: I want Students with (Zip Code = 92037 AND Gender = F) OR (ZipCode = 92101 and Gender = M)

OR

I want students with (State = CA OR State = FL) AND GPA = 4.0 AND GENDER = M

These queries are usually built using a tree control on the front end.

I currently have this working using NHibernate. The Criteria API in NHibernate is rea开发者_如何学编程lly awesome for doing this. However, NHibernate has a major bug, and that is it doesn't allow multiple joins on a 1:many table.

So for e.g. if I had a table containing a CATCODE (Category code) and an Answer, NHibernate will not currently let me do multiple querying using Criteria API.

So I cannot for e.g. do: WHERE CATCODE = A and Answer in (A,B,C) AND CATCODE = B and Answer in (V,H,Y).

Due to this limitation, I have been trying to move out of NHibernate into Entity Framework. I didn't know if there was a nice way to do this kind of stuff using APIs.

Can anyone tell me a better solution to achieve such functionality, if there is one?

Would love to hear from both NHibernate and EF experts if there are ways to solve this.


I don't really know NHibernate or any other OR/M except the entity framework but let assume you have an entity called Category, and category have relation to Answers (each category has many answers for example). So the Category entity looks like:

public class Category {
    public virtual string CATCODE{get;set}
    public virtual IList<Answer> Answers{get;set;}
}

And then you can do something like:

Categories.Where(x=>x.CATCODE =="A").Where(x=>x.Answers.Any(l=>l.Name=="1" || l.Name=="2" || l.Name==3)

I even sure there is a better way to write this query, but the point is that it is possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜