我使用 Entity Framework 生成了以下两个类
public class Persons
{
public string Name { get; set; }
public int personId { get; set; }
//NAVIGATIONL PROP.
public virtual ICollection<streetLivedIn> { get; set; }
}
public class StreetLivedIn
{
public string Address1 { get; set; }
public string AddressType { get; set; }
//NAVIGATIONL PROP.
public virtual PersonId (Foriegn Key with Person)
}
Person 和 Street Lived In 之间存在一对多关系。我正在尝试拉出一个人的列表,其 AddressType
, 在 streetlivedin
表,是“家”。为此,我有以下声明
var Lstpersons = db.persons()
.Include(x => x.streetlivedin)
.Where(x => x.streetlivedin.AddressType == "Home");
上面的代码在 where 子句中抛出一个错误,说它不能转换 ICollection<streetlivein>
至 streetlivedin
类。
我想知道如何使用 Include 和 where.And 使用 Persons 上下文来实现这一点。(我知道使用 streetLivedIn 上下文可以很容易地实现它。. 喜欢
var Lstpersons = db.streetlivedin()
.Include(x => x.person)
.Where(x => x.streetlivedin.AddressType == "Home");
(没有连接语句....请)
最佳答案
您正在尝试查找 streetlivedin.AddressType == "Home"
的位置,但 streetlivedin
是个人实体的集合。相反,在 streetlivedin
上做一个子查询,例如:
var Lstpersons = db.persons()
.Include(x => x.streetlivedin)
.Where(x => x.streetlivedin.Any(y=>y.AddressType == "Home"));
关于c# - EF 一对多 where 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37313088/