A method is not recognized by linq to entities.

.net c# entity-framework linq-to-entities

Question

I use these techniques:

   public int count(
        Guid companyId, Expression<Func<T, bool>> isMatch)
    {
        var filters = new Expression<Func<T, bool>>[]{
            x => x.PriceDefinition.CompanyId == companyId,
            isMatch
        };

        return GetCount(filters);
    }

public virtual int GetCount(
            IEnumerable<Expression<Func<T, bool>>> filters)
        {
            IQueryable<T> _query = ObjectSet;

            if (filters != null)
            {
                foreach (var filter in filters)
                {
                    _query = _query.Where(filter);
                }
            }

           return _query.Count();
        }

Use when:

count(some_guid, x => x.IsMatch(entityId, inviterId, routeId, luggageTypeId));

I get the following exception:

LINQ to Entities does not recognize the method 'Boolean IsMatch(System.Nullable`1[System.Int64], System.Nullable`1[System.Int64], System.Nullable`1[System.Int64], System.Nullable`1[System.Int64])' method, and this method cannot be translated into a store expression.

Why is this happening, exactly?
How do I fix it?

1
10
5/1/2011 2:25:34 AM

Accepted Answer

You are not allowed to utilize any arbitrary.NET methods in queries while using linq-to-entities. The query's methods must all be convertible to SQL. Returning won't assist you at all.Expession<Func<entityType, bool>> because each entry on the database server must undergo where condition evaluation.

Your code for EF denotes something like:

SELECT COUNT(*)
FROM ...
LEFT JOIN ...
WHERE IsMatch(....) 

Because EF verifies the function names supplied to the query, it will raise an exception if the SQL server doesn't provide an equivalent of IsMatch.

Only the following functions may be used with Linq-to-entities:

The methods identified with "EdmFunctions"EdmFunctionAttribute This converts a.NET function to its SQL equivalent. These functions often throw exceptions or do nothing, thus they cannot be used in standard.NET programming. They serve merely as placeholders for functions in Linq-to entities. These EdmFunctions are available:

  • EdmFunctions in advanceSystem.Data.Objects.EntityFunctions
  • Predefined EdmFunctions (not compact) for SQL ServerSystem.Data.Objects.SqlClient.SqlFunctions
  • You may import SQL functions using the custom mapped SQL functions import wizard in Entity Designer (except table valued functions). After that, you may create your own static.NET function and map it usingEdmFunction property of the designer-imported SQL function.
  • Custom model defined functions are unique functions that must be explicitly created in the EDMX file (opened as XML). It's a unique, reusable component of Entity SQL.

I've previously spoken about model specified function creation in another response. the mapping of A lot of SQL functions are similar.. Instead of making things by handFunction You will map an EDMX component.EdmFunctionAttribute attributes for an imported SQL function.

14
5/23/2017 12:14:49 PM

Popular Answer

A function called by an expression you're giving calledIsMatch .

This function is outside the comprehension of LINQ to Entities.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow