如何使用名称值集合创建动态where子句?

时间:2015-11-11 18:37:05

标签: linq dynamic where-clause namevaluecollection

我将列搜索过滤器值发送到我的web api,我无法想出如何使where子句动态化?

  

看下面(代码太多)!!

名称价值收集:

  public DataTablePager<AccountDTO> Get([FromUri] DataTableParameter param)
        {
            NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);

投放搜索值:

if (!String.IsNullOrEmpty(nvc["sSearch_0"]) && !int.TryParse(nvc["sSearch_0"], out tmpInt) ||
                !String.IsNullOrEmpty(nvc["sSearch_1"]) && !int.TryParse(nvc["sSearch_1"], out tmpInt) ||
                !String.IsNullOrEmpty(nvc["sSearch_10"]) && !int.TryParse(nvc["sSearch_10"], out tmpInt)

设置Where子句:

filteredresults = filteredresults.Where(i => CorrectNumericTypes
                                                      && (Lead_ID == null || i.Lead_ID == Lead_ID)
                                                      && (Account_ID == null || i.Account_ID == Account_ID)

2 个答案:

答案 0 :(得分:0)

这是Pipes and Filters Architectural pattern的经典案例。 您可以根据动态(运行时)条件构建过滤器列表。或者保留静态的过滤器列表,没有标准的过滤器,数据只是通过。在数据集或查询中应用此过滤器列表。

我认为你不能用LINQ实现这一目标。您可以设计查询构建器(使用相同的过滤器模式)

答案 1 :(得分:0)