如何将字符串转换为linq where子句 [英] How to convert string to linq where clause

查看:574
本文介绍了如何将字符串转换为linq where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中我有一个高级过滤功能,用户可以保存这个过滤器

现在在我的过滤器中我有FilterQuery,我在其中保存过滤器像

< pre lang =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
&&约会日期< = 20/10/2014





现在在Linq查询中它就像

 来自 cs   UnitOfWork.CaseRepository.All()
.Where(cs => cs.City = NewDelhi)



现在如何将这些字符串转换为Linq查询

-Thanks

解决方案

Hi Vishal,



您可以使用谓词。

例如:

表达式< Func< City,bool>> filterExpression = x = >  x.ServiceType =  1  2 ; 

和linq类似:
来自 cs UnitOfWork。 CaseRepository.All()。Where(filterExpression)

< / func >





您可以将过滤条件添加到表达式。



我没有对此进行测试,但您可以通过使用谓词来搜索它。



希望这有助于! :)



问候,

Praneet


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

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

from cs in UnitOfWork.CaseRepository.All()
                    .Where(cs=>cs.City=NewDelhi)


Now How i convert these string into Linq Queries
-Thanks

解决方案

Hi Vishal,

You can use the predicates for this.
For example :

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


这篇关于如何将字符串转换为linq where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆