Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
In my Project I have a function for advance filter and user can save this filter
Now in my filter I have FilterQuery in which I am saving filters Like
C#
City =NewDelhi,Faridabad,Noida,Gurgaon,GreaterNoida 
   && ServiceType=1,2 
   && NetPriceMax=1000
   && CaseStatus=1,2
   && NetPriceMax=1000 
   && CreatedDate>=20/10/2014 
   && CreatedDate<=20/10/2014 
   && AppointmentDate>=17/10/2014 
   && AppointmentDate<=20/10/2014


Now in Linq query it works like
C#
from cs in UnitOfWork.CaseRepository.All()
                    .Where(cs=>cs.City=NewDelhi)

Now How i convert these string into Linq Queries
-Thanks
Posted
Comments
BillWoodruff 20-Oct-14 11:51am    
What have you tried so far using Linq ? Are you familiar with the use of Action and Func delegates, and Lamda expressions ?

I think it would be helpful if you would add a code example of a FilterQuery you have created.
Naser Hassan 20-Oct-14 15:19pm    
How you want to use this filter ?
do you want to define and save this filter in one place so that you can use it later? if the answer is yes then try something like RuleEngine or
create a array of Func delagate

1 solution

Hi Vishal,

You can use the predicates for this.
For example :
C#
Expression<Func<City, bool>> filterExpression = x => x.ServiceType=1,2;

And the linq like :
from cs in UnitOfWork.CaseRepository.All().Where(filterExpression)

</func>


You can add your filter conditions to the expression.

I havent tested this, but you can search it this way by using predicates.

Hope this helps !! :)

Regards,
Praneet
 
Share this answer
 
v2
Comments
BillWoodruff 20-Oct-14 11:39am    
If you show a working example of this, with the ability to conditionally add boolean-clauses as strings (i.e., compose a complex query as a string, and then make that an an executable query), that you have tested, you get my #5 vote.
Naser Hassan 20-Oct-14 12:48pm    
How you want to use this filter ?
do you want to define and save this filter in one place so that you can use it later? if the answer is yes then try something like RuleEngine or
create a array of Func delagate
BillWoodruff 20-Oct-14 13:30pm    
Are you asking a question of the OP, or of the author, Praneet, of this solution ?
Naser Hassan 20-Oct-14 14:04pm    
sorry Bill ....i am asking to author who asked a question .. How to convert string to linq where clause?
BillWoodruff 20-Oct-14 15:02pm    
Hi Naser, May I suggest you add comments to the OP as comments on the OP's question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900