linq to entities does not recognize the method System.Collections.Generic.List

entity-framework linq

Accepted Answer

What you wrote will attempt to be translated

x.Addresses.Where(a => a.AddressSubTypeId == 1).Select(a => new Model.Phone
{
    PhoneId = a.AddressId,
    Name = a.CompleteAddress
}).ToList()

and

Emailes = x.Addresses.Where(a => a.AddressSubTypeId == 2).Select(a => new Model.Email
{
    EmailId = a.AddressId,
    Name = a.CompleteAddress
}).ToList()

it fails when used in a SQL statement.

Get Persons into memory first, then use the aforementioned select after that. You can call and get people.ToList() or AsEnumerable() These calls will materialize the query, after which the lists can be used.

return context.Persons.ToList().Select(x => new Model.Person
    {
        PersonId = x.PersonId,
        Name = x.FirstName,
        LastName = x.LastName,
        Phones = x.Addresses.Where(a => a.AddressSubTypeId == 1).Select(a => new Model.Phone
            {
                PhoneId = a.AddressId,
                Name = a.CompleteAddress
            }).ToList(),
        Emailes = x.Addresses.Where(a => a.AddressSubTypeId == 2).Select(a => new Model.Email
            {
                EmailId = a.AddressId,
                Name = a.CompleteAddress
            }).ToList()

    }).ToList();

Remember that callingToList() , will load all records into memory, potentially placing a severe burden on the server's memory. This might not be a problem in your instance since you would already pick all rows, but you should think about including aWhere clause and possibly aSelect before contacting, consider plain fieldsToList() to ensure that you only receive what you require and not the entire table.

4
10/31/2013 10:11:04 AM

Popular Answer

It is not possible to materialize the queries to which you are making reference first and then utilize the materialized lists in the queries.



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