我有以下实体:
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])' 方法,并且此方法无法转换为存储表达式。