【问题标题】:linq to entities does not recognize the method System.Collections.Generic.Listlinq to entity 无法识别 System.Collections.Generic.List 方法
【发布时间】:2013-10-31 09:58:13
【问题描述】:

我有以下实体:

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.List1[Model.Phone] ToList[Phone](System.Collections.Generic.IEnumerable1[Model.Phone])' 方法,并且该方法无法转换为存储表达式。

【问题讨论】:

    标签: linq entity-framework


    【解决方案1】:

    你写的东西会尝试翻译

    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 语句中,它失败了。

    您应该首先在内存中获取 Persons,然后应用上述选择。您可以通过调用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() 将获取内存中的所有记录,这可能会导致服务器内存的大量使用。在您的情况下,您无论如何都会选择所有行,所以这可能不是问题,但您应该考虑在调用 ToList() 之前为普通字段添加一个 Where 子句甚至是一个 Select ,以便得到您想要的需要,而不是整张桌子。

    【讨论】:

      【解决方案2】:

      您不能在表达式中调用方法,先具体化您引用的查询,然后在查询中使用具体化列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多