我有一个 LINQ 查询,我无法弄清楚为什么它不返回任何结果,即使条件为真。
我传入TBL_CONTACTAddress contactAddress, pPeople person
我的方法。
我的 LINQ 查询如下所示:
pPeople_Address recordExists = people
.Select(p => p.pPeople_Address
.Where(a => a.People_ID == person.ID
&& a.Address1 == contactAddress.LINE1)
.FirstOrDefault())
.FirstOrDefault();
如果我将鼠标悬停在最后一个FirstOrDefault()
它说它正在返回pPeople_Address
。当我对其进行调试时,两者person.ID
都contactAddress.LINE1
提供了正确的输入。该记录存在于IEnumerable<pPeople> people
.
我在这里遗漏了什么?
EDIT1:第一部分获取实体。
using (CRMEntities crm = new CRMEntities())
{
people = crm.pPeoples.Where(p => p.import_num == ImportNumber).Select(p => p).AsEnumerable().ToList();
}
第二部分是更新信息并查看记录是否存在
private void GetAddressGeneric(TBL_CONTACT contact, pPeople person, CRMEntities crm)
{
foreach (TBL_ADDRESS contactAddress in contact.TBL_ADDRESS.Where(n => n.LINE1 != null))
{
var address = new pPeople_Address();
pPeople_Address recordExists = people.Select(p => p.pPeople_Address.FirstOrDefault(a => a.People_ID == person.ID && a.Address1 == contactAddress.LINE1)).FirstOrDefault();
}
EDIT2:下面的代码完成了我想要的,但我希望能够在一个 LINQ 语句中完成
var recordExists = people.FirstOrDefault(p => p.pPeople_Address.Any(a =>
a.People_ID == person.ID &&
a.Address1 == contactAddress.LINE1));
pPeople_Address record = recordExists.pPeople_Address.FirstOrDefault();