LINQ 动态查询库 [英] LINQ Dynamic Query Library
问题描述
我正在使用 Entity Framework 4 构建一个 ASP.Net MVC 3 应用程序.执行以下两段代码时,两个变量(query1 和 query2)的返回类型均为
I am building an ASP.Net MVC 3 application with Entity Framework 4. When the two pieces of code below are executed, both variables (query1 and query2) have a return type of
System.Data.Objects.ObjectQuery<Asset.Model.Equipment>
Query1 使用 ObjectContext 的直接实例,而 Query2 使用存储库模式,即它在 EquipmentService 中调用 GetEquipment,而后者又在 Equipment Repository 中调用相同命名的方法.Service 和 Repository 中的方法都返回
Query1 uses a direct instance of the ObjectContext, however, Query2 uses a repository pattern, ie, it calls GetEquipment in EquipmentService, which in turns calls the same named method in Equipment Repository. Both the methods in the Service and Repository return
IQueryable<Equipment>
这是我的问题,为什么 query2 仅在我包含时才有效
How, here's my question, how come query2 will only work when I include
using System.Linq.Dynamic;
在我的控制器顶部
using (AssetEntities context = new AssetEntities())
{
var query1 = context.Equipments
.OrderBy("it." + sidx + " " + sord)
.Skip(pageIndex * pageSize)
.Take(pageSize);
}
var query2 = equipService.GetEquipment()
.OrderBy(sidx + " " + sord)
.Skip(pageIndex * pageSize)
.Take(pageSize);
如果我从控制器中省略 System.Linq.Dynamic,我会在 Query2 中收到错误
If I omitt System.Linq.Dynamic from my controller, I get an error within Query2 at
.OrderBy(sidx + " " + sord)
哪些州
The type arguments for method 'System.Linq.Queryable.OrderBy<TSource,TKey>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,TKey>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly
有谁知道为什么 query1 可以在不使用 System.Linq.Dynamic 的情况下工作,但 query2 需要它来执行?
Does anyone know why query1 can work without having to use System.Linq.Dynamic, but that query2 needs it to execute?
谢谢大家.
推荐答案
在第一个查询中,context.Equipments
的类型为 ObjectQuery
.ObjectQuery<T>
有方法 OrderBy(string) 需要 .OrderBy("it." + sidx + " " + sord)
.所以第一个查询工作.
In the first query context.Equipments
has type ObjectQuery<Equipment>
. The ObjectQuery<T>
has the method OrderBy(string) which one need for .OrderBy("it." + sidx + " " + sord)
. So the first query work.
在第二个查询中,您使用 IQueryable
类型的 equipService.GetEquipment()
.IQueryable<T>
只有扩展方法 OrderBy 使用 Expression
作为参数,而不是 string
.因此,要将 OrderBy
与 IQueryable<Equipment>
一起使用,您必须编写类似
In the second query you use equipService.GetEquipment()
of the type IQueryable<Equipment>
. The IQueryable<T>
has only extension method OrderBy with Expression<Func<T, TKey>>
as the parameter instead of string
. So to use OrderBy
with IQueryable<Equipment>
you have to write something like
equipService.GetEquipment().OrderBy(e => e.equipmentID)
但这不是你可以使用的.您需要另一种扩展方法,它可以为您提供 System.Linq.Dynamic
形式的 LINQ 动态查询库.
but it is not what you can use. To you need another extension method, which can provide you the LINQ Dynamic Query Library in form System.Linq.Dynamic
.
在许多情况下,LINQ to Entities 有许多限制,但是在您的情况下,它与 LINQ to SQL 相比具有更多优势.所以我建议你在你的情况下继续使用 LINQ to Entities.我相信,由于直接在您使用的实体框架中对所有功能的原生支持,您将获得更好的性能.
In many cases LINQ to Entities has many restrictions, but in your case it has more advantages as LINQ to SQL. So I recommend you to stay by LINQ to Entities in your case. I am sure that in the way you will receive better performance because of native support of all function directly in the Entity Framework which you use.
因为 LINQ to Entities 或 ObjectQuery
支持 Where(string)
方法(确切地说是 ObjectQuery.Where(string predicate, params ObjectParameter[] parameters) method) 你可以相对容易地在 jqGrid 中实现过滤/搜索..Where
的用法可以是
Because LINQ to Entities or ObjectQuery<Equipment>
supports Where(string)
method (to be exactly ObjectQuery.Where(string predicate, params ObjectParameter[] parameters) method) you can relatively easy implement filtering/searching in jqGrid. The usage of .Where
can be
.Where("it.equipmentID < 100")
或
.Where("it.equipmentID < @maxId", new ObjectParameter ("maxId", 100))
例如(在 ObjectParameter
中使用maxId"而不是@maxId"不是输入错误).
for example (the usage of "maxId" instead of "@maxId" in the ObjectParameter
is not typing error).
更新:在 答案你可以找到示例展示了如何根据我上面描述的想法在 jqGrid 中实现过滤/搜索.
UPDATED: In "UPDATED" part of the answer you can find the example which shows how to implement filtering/searching in jqGrid based on the idea which I described above.
这篇关于LINQ 动态查询库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!