LINQ Dynamic Query Library

2019-01-18 14:42发布

问题:

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.

回答1:

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.



回答2:

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