EntitySet.Where(myPredicate) throws NotSupportedException

前端 未结 3 915
不知归路
不知归路 2021-01-18 15:34

EDIT: Let\'s try this again. This time I\'ve used the AdventureWorks sample database so you can all play along. This will rule out anything crazy I\'ve do

3条回答
  •  终归单人心
    2021-01-18 16:12

    I don't fully understand the guts of Linq to Entities, but there is an Open Source (usable in proprietary software) toolkit specifically designed to help solve this problem, called LinqKit, linked off this O'Reilly-related article:

    http://www.albahari.com/nutshell/predicatebuilder.aspx

    Since I don't fully understand the guts, I'll just quote them:

    Entity Framework's query processing pipeline cannot handle invocation expressions, which is why you need to call AsExpandable on the first object in the query. By calling AsExpandable, you activate LINQKit's expression visitor class which substitutes invocation expressions with simpler constructs that Entity Framework can understand.

    Here is a direct link to LinqKit.

    And here is the type of code that this project enables:

    using LinqKit;
    
    // ...
    
    Expression> expression = product => product.ListPrice > 0;
    
    var result = db.ProductSubcategories
        .AsExpandable() // This is the magic that makes it all work
        .Select(
            subCategory => new
            {
                Name = subCategory.Name,
                ProductArray = subCategory.Products
                    // Products isn't IQueryable, so we must call expression.Compile
                    .Where(expression.Compile())
            })
        .First();
    
    Console.WriteLine("There are {0} products in SubCategory {1} with ListPrice > 0."
        , result.ProductArray.Count()
        , result.Name
        );
    

    The result is:

    There are 3 products in SubCategory Bib-Shorts with ListPrice > 0.

    Yay, no exception, and we can extract the predicate!

提交回复
热议问题