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

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

问题描述

将动态 WHERE 子句组装到 LINQ 语句的最佳方法是什么?

我在一个表单上有几十个复选框,并将它们作为以下内容传回:Dictionary>(Dictionary>) 到我的 LINQ 查询.

public IOrderedQueryableGetProductList(string productGroupName, string productTypeName, Dictionary> filterDictionary){var q = from c in db.ProductDetail其中 c.ProductGroupName == productGroupName &&c.ProductTypeName == productTypeName//在这里插入动态过滤器orderby c.ProductTypeName选择 c;返回 q;}

解决方案


(来源:scottgu.com)

你需要这样的东西吗?使用 Linq 动态查询库(下载包括示例).>

查看ScottGu 的博客 了解更多示例.

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;
}

解决方案


(source: scottgu.com)

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天全站免登陆