LINQ动态查询库 [英] LINQ Dynamic Query Library
问题描述
System.Data.Objects.ObjectQuery&Asset.Model.Equipment>
Query1使用ObjectContext的直接实例,但是Query2使用存储库模式,即调用GetEquipment在EquipmentService中,它们在设备存储库中调用相同的命名方法。服务和存储库中的方法都返回
IQueryable< Equipment>
这是我的问题,当我包含
$ b时,query2将如何工作$ b
使用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中发现错误, p>
.OrderBy(sidx ++ sord)
哪些状态
方法'System.Linq.Queryable.OrderBy< ;不能从使用中推断出TSource,TKey>(System.Linq.IQueryable< TSource>,System.Linq.Expressions.Expression< System.Func< TSource,TKey>>)。有没有人知道为什么query1可以工作而不必使用System.Linq?动态的,但是query2需要它执行?
。所以要使用
谢谢大家。
解决方案在第一个查询
context.Equipments
中有ObjectQuery< Equipment>
。ObjectQuery< T>
具有方法 OrderBy(string)需要.OrderBy(it。+ sidx ++ sord)
。所以第一个查询工作。
在第二个查询中,您使用
equipService.GetEquipment()
类型的IQueryable<设备>
。IQueryable< T>
只有扩展方法的表达式< Func< T,TKey>>
作为参数的.aspxrel =nofollow noreferrer> OrderBy 串OrderBy
与IQueryable< Equipment>
你必须写一些类似equipService.GetEquipment()。OrderBy(e => e.equipmentID)
但这不是你可以使用的。您需要另一种扩展方法,它可以以
System.Linq.Dynamic
的形式为您提供LINQ动态查询库。
在许多情况下,LINQ to Entities有许多限制,但在你的案例有更多的优点,如LINQ to SQL。所以我建议你留在你的案件中的LINQ to Entities。我确信,您将会获得更好的表现,因为您直接在您使用的实体框架中支持所有功能。
因为LINQ to Entities或
ObjectQuery< Equipment>
支持Where(string)
方法(要准确地 ObjectQuery.Where(string predicate,params ObjectParameter [] parameters))方法)你可以在jqGrid中比较容易地实现过滤/搜索。的使用。
可以.Where(it设备ID <100)
或
.Where(it.equipmentID <@maxId,新的ObjectParameter(maxId,100))
例如(
ObjectParameter
中的maxId而不是@maxId的使用不会输入错误) / p>
更新:在更新的答案你可以找到示例,其中显示了如何在jqGrid中实现过滤/搜索,基于上述的想法。
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 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>
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);
If I omitt System.Linq.Dynamic from my controller, I get an error within Query2 at
.OrderBy(sidx + " " + sord)
Which states
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
Does anyone know why query1 can work without having to use System.Linq.Dynamic, but that query2 needs it to execute?
Thanks Everyone.
解决方案In the first query
context.Equipments
has typeObjectQuery<Equipment>
. TheObjectQuery<T>
has the method OrderBy(string) which one need for.OrderBy("it." + sidx + " " + sord)
. So the first query work.In the second query you use
equipService.GetEquipment()
of the typeIQueryable<Equipment>
. TheIQueryable<T>
has only extension method OrderBy withExpression<Func<T, TKey>>
as the parameter instead ofstring
. So to useOrderBy
withIQueryable<Equipment>
you have to write something likeequipService.GetEquipment().OrderBy(e => e.equipmentID)
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
.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.
Because LINQ to Entities or
ObjectQuery<Equipment>
supportsWhere(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")
or
.Where("it.equipmentID < @maxId", new ObjectParameter ("maxId", 100))
for example (the usage of "maxId" instead of "@maxId" in the
ObjectParameter
is not typing error).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屋!