System.Linq.Dynamic does not work for Entity Framework

I'm trying to use the LINQ dynamic query library hosted here - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library .aspx

It should work for EF, but I can not get it to fulfill this requirement.

The following works fine:

List<string> paramsList = new List<string> {"CustomerID"};
        var customer =
            ctx.Customers.Where(cus=>cus.CompanyName.Contains("A")).Select("new(" +
                                 string.Join(", ", paramsList.ToArray()) +
                                 ")");     

However, if I omit the Where clause and do something like this

List<string> paramsList = new List<string> {"CustomerID"};
        var customer =
            ctx.Customers.Select("new(" +
                                 string.Join(", ", paramsList.ToArray()) +
                                 ")");     

I get the following error:

'new' cannot be resolved into a valid constructor or function of type., next to the constructor of a method, method, or type

It works fine if I use Linq2Sql instead of Linq2Entities.

What am I missing here?

+3
source share
2 answers

, - , :

ctx.Customers - ObjectSet, Dynamic Linq.

, - .Contains(), IQueryable, .

IQueryable, :

ctx.Customers.AsQueryable().Select(...)
+2

Source: https://habr.com/ru/post/1788058/


All Articles