LINQ Dynamic Query Library

淺唱寂寞╮ 提交于 2019-11-28 10:15:26
Oleg

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!