linq to entities无法识别方法System.Collections.Generic.List

时间:2013-10-31 09:58:14

标签: linq entity-framework

我有以下实体:

public class Person
{
    #region Primitive Properties
    public virtual int PersonId {get; set;}
    public virtual string FirstName{get; set;}
    public virtual string LastName { get; set; }
    #endregion

    #region Navigation Projperties
    public virtual ICollection<Address> Addresses { get; set; }
    #endregion
}

public class Address
{
    #region primitive properties
    public virtual int AddressId
    {
        get;
        set;
    }
    public virtual int PersonId
    {
        get;
        set;
    }
    public virtual int AddressSubTypeId
    {
        get;
        set;
    }
    public virtual string CompleteAddress
    {
        get;
        set;
    }
    public virtual bool IsActive
    {
        get;
        set;
    }
    #endregion

}

并有以下型号:

public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public List<Email> Emailes { get; set; }
    public List<Phone> Phones { get; set; }
}

public class Phone
{
    public int PhoneId { get; set; }
    public string Name { get; set; }
}


public class Email
{
    public int EmailId { get; set; }
    public string Name { get; set; }
}

我想通过以下代码获取我的人员名单:

 return context.Persons.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();

当我在上面运行表达式时出现错误:

  

LINQ to Entities无法识别方法'System.Collections.Generic.List 1[Model.Phone] ToList[Phone](System.Collections.Generic.IEnumerable 1 [Model.Phone])'方法,并且此方法无法转换为商店表达式。

2 个答案:

答案 0 :(得分:4)

您所写的内容将尝试翻译

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()

在SQL语句中失败。

首先应该让人在内存中然后应用上面的选择。您可以通过致电ToList()AsEnumerable()来获取人员...这些调用将实现查询,然后就可以应用这些列表。

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();

请记住,调用ToList()将获取内存中的所有记录,这可能会导致服务器内存的大量使用。在您的情况下,您无论如何都会选择所有行,因此这可能不是问题,但在调用Where之前,您应该考虑为普通字段添加Select子句甚至ToList()为了得到你需要的东西,而不是整张桌子。

答案 1 :(得分:2)

您无法调用表达式中的方法,首先实现您引用的查询,然后在查询中使用实现列表。