Creating dynamic queries with entity framework?

You could compose an IQueryable GetQuery(FilterDefinition filter) { IQueryable query = context.Set(); // assuming that you return all records when nothing is specified in the filter if (filter. FilterByName) query = query. Where(t => t.Name >= filter.

NameFrom && t. Name t. Quantity >= filter.

QuantityFrom && t. Quantity QuantityTo); return query; }.

You could compose an IQueryable step by step. Assuming you have a FilterDefinition class which describes how the user wants to filter ... public class FilterDefinition { public bool FilterByName { get; set; } public string NameFrom { get; set; } public string NameTo { get; set; } public bool FilterByQuantity { get; set; } public double QuantityFrom { get; set; } public double QuantityTo { get; set; } } ... then you could build a query like so: public IQueryable GetQuery(FilterDefinition filter) { IQueryable query = context.Set(); // assuming that you return all records when nothing is specified in the filter if (filter. FilterByName) query = query.

Where(t => t. Name >= filter. NameFrom && t.Name t.

Quantity >= filter. QuantityFrom && t.Quantity.

– Eduard Apr 5 at 6:36 @t-edd: No, it leverages deferred execution (blogs.msdn.Com/b/charlie/archive/2007/12/09/…). That means that IQueryable which is composed in the example above is only a query expression which describes how the data are filtered. The real execution of the query isn't in the example at all.

You execute the query then by applying a "greedy" operator to IQueryable, for instance query.ToList(). At this point - and not earlier - the query expression is translated into SQL and sent to the server. – Slauma Apr 5 at 10:03 Very nice, I didn't know that, thank you.Im a beginer in .

Net comming from java enviroment... – Eduard Apr 5 at 14:04.

The only other way that I know of would be to build an IQueryable based on your filter vaues. Public List Get(FilterValues filter) { using (var context = new AdventureWorksEntities()) { IQueryable query = context.Contacts. Where(c => c.

ModifiedDate > DateTime. Now); if (!string. IsNullOrEmpty(filter.

FirstName)) { query = query. Where(c => c. FirstName == filter.

FirstName); } if (!string. IsNullOrEmpty(filter. LastName)) { query = query.

Where(c => c. LastName == filter. LastName); } return query.ToList(); } }.

Imagine I have very large set of data.... – Eduard Apr 5 at 6:38 No, it's not a performance hit, as it uses deferred execution to only query once. – BrandonZeider Apr 5 at 12:54 +1 Thank you for good answer. – Eduard Apr 5 at 14:05.

You could look into creating the service using WCF Data Services and dynamically create the URI to query your entity model.

I would like to know what is the best way of creating dynamic queries with entity framework and linq. I want to create a service that has many parameters for sorting and filtering (over 50). I will be getting object from gui where these will be filled out... and query will be executed from a single service method.

I looked around And I saw that I could dynamically create a string that can be executed at the end of my method. I don't like this way very much. Is there a better way to do this?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions