Friday, October 20, 2017

LINQ To EF : Error "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities in entity framework"

In LINQ -to-EF query, First EF query is translated into SQL and when we are trying to translate some code into SQL and something is not supported in SQL and then this exception occurred.




"The LINQ expression node type 'Invoke' is not supported in LINQ to Entities in entity framework"

    public List<PersonData> GetAll()

        {

             return _db.Persons.Select(x => MapToPersonData(x)).ToList();

        }

internal static Func<Person, PersonData> MapToPersonData = m =>
    {
        if (m == null)
        {
            return null;
        }
        else
        {
            return new PersonData()
            {
              PersonID = m.PersonID,
              Name = m.Name,
              Location = m.Location.City +""+ m.Location.Street
            };
        }
    };


In above EF Query, the function MapToPersonData()  is being used to map the person entity into person data object and in this function, we are concatenating  of the city and Street string and the concatenating of string will not be supported in LINQ to entties, so the above error is occured.

What solution I found, we have to fetch data from database table in your local memory by calling .ToList()/.ToEnumerable() function and then we can apply logic on top of that similar like LINQ-To-Object 


       public List<PersonData> GetAll()

        {

           return _db.Persons.AsEnumerable().Select(x => MapToNameData(x)).ToList();

        }

  
Other entity framework related links

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...