LINQ动态查询库

发布于 2024-10-19 13:16:40 字数 1417 浏览 1 评论 0 原文

我正在使用 Entity Framework 4 构建 ASP.Net MVC 3 应用程序。当执行下面的两段代码时,两个变量(query1 和 query2)的返回类型均为

System.Data.Objects.ObjectQuery<Asset.Model.Equipment>

Query1 使用 ObjectContext 的直接实例,但是 Query2 使用存储库模式,即它调用 EquipmentService 中的 GetEquipment,后者又调用 Equipment Repository 中的相同命名方法。服务和存储库中的方法都返回

IQueryable<Equipment>

How,这是我的问题,为什么 query2 仅当我包含

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 中收到错误

.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 需要它来执行?

谢谢大家。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东北女汉子 2024-10-26 13:16:40

在第一个查询中,context.Equipments 的类型为 ObjectQueryObjectQuery 具有方法 OrderBy(string ) 其中需要 .OrderBy("it." + sidx + " " + sord)。所以第一个查询工作了。

在第二个查询中,您使用 IQueryable 类型的 equipService.GetEquipment()IQueryable 仅具有扩展方法 OrderByExpression> 作为参数而不是 string。因此,要将 OrderByIQueryable 一起使用,您必须编写类似的内容

equipService.GetEquipment().OrderBy(e => e.equipmentID)

,但它不是您可以使用的内容。您需要另一种扩展方法,它可以为您提供 System.Linq.Dynamic 形式的 LINQ 动态查询库。

在许多情况下,LINQ to Entities 有许多限制,但在您的情况下它比 LINQ to SQL 有更多的优点。因此,在您的情况下,我建议您继续使用 LINQ to Entities。我确信,由于您使用的实体框架直接对所有功能进行本机支持,因此您将获得更好的性能。

因为 LINQ to Entities 或 ObjectQuery 支持 Where(string) 方法(确切地说是 ObjectQuery.Where(string predicate, params ObjectParameter[]parameters) 方法)您可以相对轻松地在 jqGrid 中实现过滤/搜索。 .Where 的用法可以是

.Where("it.equipmentID < 100")

.Where("it.equipmentID < @maxId", new ObjectParameter ("maxId", 100))

例如(在 ObjectParameter 中使用“maxId”而不是“@maxId”不是打字错误)。

更新:在答案你可以找到示例 它展示了如何根据我上面描述的想法在 jqGrid 中实现过滤/搜索。

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.

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)

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> 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")

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.

柠檬色的秋千 2024-10-26 13:16:40

“it”是默认的 ObjectQuery.Name 属性值。事实上,当您使用第一个查询时,您执行隐式 Entity SQL Order By 子句,而在第二个查询中您使用 LINQ to Entities,并且它需要 System.Linq.Dynamic 命名空间才能正常工作。

"it" is the default ObjectQuery.Name property value. In fact, when you are using the first query, you perform an implicit Entity SQL Order By clause, while in the second one you are using LINQ to Entities, and it needs System.Linq.Dynamic namespace to work correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文