在LINQ动态WHERE子句 [英] Dynamic WHERE clause in LINQ

查看:194
本文介绍了在LINQ动态WHERE子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是组建一个动态的最佳途径WHERE子句到LINQ声明?

我有一个表格上几十个复选框和我路过他们回来为:字典<字符串列表<串GT;> (词典<字段名,列表与LT;值>&GT)。我的LINQ查询

 公共IOrderedQueryable<&产品详情GT; GetProductList(字符串productGroupName,串productTypeName,字典<字符串列表<串GT;> filterDictionary)
{
    变种Q =从C在db.ProductDetail
            其中,c.ProductGroupName == productGroupName&放大器;&安培; c.ProductTypeName == productTypeName
            //此处插入动态过滤器
            排序依据c.ProductTypeName
            选择C;
    返回q;
}


解决方案

您需要这样的事?使用 LINQ的动态查询库(下载包括示例)。

查看<一个href=\"http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx\">ScottGu's博客更多的例子。

What is the best way to assemble a dynamic WHERE clause to a LINQ statement?

I have several dozen checkboxes on a form and am passing them back as: Dictionary<string, List<string>> (Dictionary<fieldName,List<values>>) to my LINQ query.

public IOrderedQueryable<ProductDetail> GetProductList(string productGroupName, string productTypeName, Dictionary<string,List<string>> filterDictionary)
{
    var q = from c in db.ProductDetail
            where c.ProductGroupName == productGroupName && c.ProductTypeName == productTypeName
            // insert dynamic filter here
            orderby c.ProductTypeName
            select c;
    return q;
}

解决方案

You need something like this? Use the Linq Dynamic Query Library (download includes examples).

Check out ScottGu's blog for more examples.

这篇关于在LINQ动态WHERE子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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