2

我有以下实体:

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.IEnumerable1[Model.Phone])' 方法,并且此方法无法转换为存储表达式。

4

2 回答 2

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

您应该首先在内存中获取 Persons,然后应用上述选择。您可以通过调用来获取 PersonsToList()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()以便获得您需要的确切内容,而不是整个表。

于 2013-10-31T10:01:54.270 回答
2

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

于 2013-10-31T10:01:03.300 回答