Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 448
tcode Avatar asked Feb 28 '11 10:02

tcode


People also ask

What is Dynamic LINQ?

The Dynamic LINQ library exposes a set of extension methods on IQueryable corresponding to the standard LINQ methods at Queryable, and which accept strings in a special syntax instead of expression trees.

Is Dynamic LINQ safe?

And Dynamic Linq is actually composed from strings, therefore it is potentially prone to attack by injection. Obviously, the attacker will have to be aware of the fact that you are using DynamicLinq and could attack only preparing the data so it results in valid malicious Dynamic Linq query.

What is System LINQ dynamic core?

System.Linq.Dynamic is the Microsoft assembly for the .Net 4.0 Dynamic language functionality. System.Linq.Dynamic.Core is a .NET Core port of the the Microsoft assembly for the .Net 4.0 Dynamic language functionality.


1 Answers

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.

like image 98
Oleg Avatar answered Sep 22 '22 08:09

Oleg