LINQ动态查询库 [英] LINQ Dynamic Query Library
问题描述
我建立与实体框架4.一个ASP.Net MVC 3应用程序当code两片下面执行时,这两个变量(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>
查询1使用的ObjectContext的直接实例,但是,QUERY2使用存储库模式,也就是说,它在EquipmentService,这反过来调用设备库名称相同的方法调用GetEquipment。无论是在服务和回报库方法
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;
在我的控制器顶部
At the top of my controller
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);
如果我从我的控制器omitt 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&LT;设备&GT;
。在的ObjectQuery&LT; T&GT;
有方法的的OrderBy(串)其中一个需要 .OrderBy(吧。+ 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.
在你使用 equipService.GetEquipment()
类型的第二个查询的IQueryable&LT;设备&GT;
。在的IQueryable&LT; T&GT;
只有扩展方法的排序依据与防爆pression&LT; Func键&LT; T,TKEY的&GT;&GT;
作为参数,而不是字符串
。因此,要使用排序依据
与的IQueryable&LT;设备&GT;
你必须写像
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)
但它不是你可以使用的东西。为了你需要另一个分机方法,它可以为您提供LINQ动态查询库中的形式 System.Linq.Dynamic
。
在很多情况下,LINQ到实体拥有诸多限制的,但在你的情况下,它有更多的优点的LINQ to SQL。所以,我建议你留在LINQ到实体在您的案件。我相信,在路上你会收到,因为直接在您使用实体框架的原生支持所有功能更好的性能。
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到实体或的ObjectQuery&LT;设备&GT;
支持其中(串)
方法(以完全相同<一个HREF =http://msdn.microsoft.com/en-us/library/bb338811.aspx相对=nofollow> ObjectQuery.Where(字符串predicate,则params ObjectParameter []参数)方法),你可以比较容易实现过滤/在jqGrid的搜索。 。凡
的用法可能是
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))
例如(maxId的用法,而不是@maxId中的 ObjectParameter
不是打字错误)。
for example (the usage of "maxId" instead of "@maxId" in the ObjectParameter
is not typing error).
更新时间::的the回答你可以找到 足见例如如何实现过滤/搜索在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屋!