Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

entity framework 4 - LINQ Dynamic Query Library

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...