LINQ查询不返回结果

时间:2013-10-29 16:34:23

标签: c# linq

我有一个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.IDcontactAddress.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();

2 个答案:

答案 0 :(得分:0)

如果您在单个Linq请求中尝试获取与指定的person.IDcontactAddress.LINE1值匹配的第一个地址条目(任何人),那么您应该尝试以下代码:< / p>

pPeople_Address recordExists = people.SelectMany(p => p.pPeople_Address)
                                     .FirstOrDefault(a => a.People_ID == person.ID &&
                                                          a.Address1 == contactAddress.LINE1);

答案 1 :(得分:0)

首先,我会将你的表达式重写为:

    pPeople_Address recordExists = 
        people.Where(a => a.People_ID == person.ID && a.Address1 == contactAddress.LINE1)
        .Select(p => p.pPeople_Address)
        .FirstOrDefault();

如果您仍未获得所需的结果,可以尝试调试一些中间变量以找出问题所在。例如:

    var matchingPeople = 
        people.Where(a=> a.People_ID == person.ID && a.Address1 == contactAddress.LINE1).ToList(); // Depending on your data, calling ToList() on this expression may not be feasible
    var matchingAddresses = 
        matchingPeople.Select(p => p.pPeople_Address).ToList();
    var firstMatchingAddress = 
        matchingAddresses.FirstOrDefault();

注意:LINQ查询操作是延迟的,这意味着在您尝试实际使用数据之前,不会执行查询。这就是为什么需要像ToList()和FirstOrDefault()这样的调用来获得具体结果的原因。

编辑:根据您的“编辑2”,我相信您正在寻找的是

    pPeople_Address record = (from p in people
                             from a in p.people_Address
                             where a.People_ID == person.ID && a.Address1 == contact.Address.LINE1
                             select a).FirstOrDefault();

很抱歉查询语法,但在我看来,这是我认为你正在尝试的最干净的方法。