有没有使用LINQ动态创建一个过滤器的模式? [英] Is there a pattern using Linq to dynamically create a filter?

查看:171
本文介绍了有没有使用LINQ动态创建一个过滤器的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有使用LINQ动态创建一个过滤器的模式?

Is there a pattern using Linq to dynamically create a filter?

我需要创建自定义筛选名单上,在过去,我只想动态创建SQL ...这似乎不像这是可能使用LINQ。

I have the need to create custom filtering on a list, in the past I would just dynamically create the SQL...it doesn't seem like this is possible with Linq.

推荐答案

退房的<一个href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx">Dynamic LINQ的图书馆从ScottGu的博客:

例如,下面是一个标准的类型安全的LINQ to SQL VB查询检索数据从Northwind数据库,并在ASP.NET GridView控件中显示出来:

For example, below is a standard type-safe LINQ to SQL VB query that retrieves data from a Northwind database and displays it in a ASP.NET GridView control:

Dim Northwind As New NorthwindDataContext
Dim query = From q In Northwind.Products Where p.CategoryID = 2 And p.UnitPrice > 3 Order By p.SupplierID Select p

Gridview1.DataSource = query
GridView1.DataBind()

使用LINQ DynamicQuery库,我可以重新写上面的查询EX pression,而不是像这样

Using the LINQ DynamicQuery library I could re-write the above query expression instead like so

Dim Northwind As New NorthwindDataContext
Dim query = Northwind.Products .where("CategoryID=2 And UnitPrice>3") . OrderBy("SupplierId")
Gridview1.DataSource = query
GridView1.DataBind()

请注意条件,where子句和排序,排序依据子句现在采取的字符串前pressions代替code EX pressions。因为它们是后期绑定字符串,我可以动态地构建它们。例如:我可以用我的应用程序,使他们能够构造查询自己(包括任意的条件子句)提供用户界面,最终用户业务分析师。

Notice how the conditional-where clause and sort-orderby clause now take string expressions instead of code expressions. Because they are late-bound strings I can dynamically construct them. For example: I could provide UI to an end-user business analyst using my application that enables them to construct queries on their own (including arbitrary conditional clauses).

这篇关于有没有使用LINQ动态创建一个过滤器的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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