简体   繁体   English

如何将这两个linq查询合并到一个查询中?

[英]How do I combine these two linq queries into a single query?

How can I get this in to one query? 如何将其添加到一个查询中? What I want is the Person from the Company that matches the name of the person I'm searching for. 我想要的是公司的人员与我正在搜索的人的姓名相匹配。

Currently I get the Company and then run basically the same search. 目前我得到公司,然后运行基本相同的搜索。

var existingCompany = bidInfo.Companies
        .FirstOrDefault( c=> c.CompanyDomain != null && 
                           c.CompanyDomain.People.FirstOrDefault( 
                                 p => p.Name == bidInfo.ArchitectPerson.Name ) 
                                        != null);
Person existingPerson=null;
if (existingCompany !=null)
{
     existingPerson = existingCompany.CompanyDomain.People.FirstOrDefault(p => p.Name == bidInfo.ArchitectPerson.Name);
}

Assuming I understand what you're trying to do, you could do something like this: 假设我明白你要做什么,你可以这样做:

var person = bidInfo.Companies
            .Where(c=>c.CompanyDomain != null)
            .SelectMany(c=>c.CompanyDomain.People)
            .SingleOrDefault(p=>p.Name == bidInfo.ArchitectPerson.Name);

Note that you're already not filtering on the company (you're just getting the first company that has someone with that name, what if there are multiples? If that's not possible then the company check is useless and you may as well do as I do and just select all people then filter on that instead of going inside of each company, checking if the person is there, then somehow going up and back down!) 请注意,您已经没有过滤公司了(您只是让第一家拥有该名称的公司,如果有倍数会怎么样?如果不可能那么公司支票也没用,您也可以这样做我做了,然后选择所有人然后过滤,而不是进入每个公司内部,检查这个人是否在那里,然后以某种方式上升和下降!)

In order to find the Person from the Company that matches the name of the bidInfo.ArchitectPerson you're searching for, you will need to look at all the people in the companies associated with the bidInfo , and then find the person with the matching name. 为了找到PersonCompany的名称相匹配bidInfo.ArchitectPerson你正在寻找,你需要看看在与相关公司所有的人bidInfo ,然后找到匹配的名字的人。

This can be accomplished with the following: 这可以通过以下方式完成:

var existingPerson = bidInfo.Companies
    .Where(c => c.CompanyDomain != null && c.CompanyDomain.People != null)
    .SelectMany(c => c.CompanyDomain.People)
    .FirstOrDefault(p => p.Name == bidInfo.ArchitectPerson.Name)


FirstOrDefault vs. SingleOrDefault : FirstOrDefaultSingleOrDefault

Names are unlikely to be unique: one company could have more than one "John Smith" working for them; 名称不太可能是唯一的:一家公司可能有一个以上的“约翰史密斯”为他们工作; one bidInfo could contain several Companies , more than one of whom each employ a different "Jane Smith". 一个bidInfo可能包含多个Companies ,其中不止一个Companies雇用不同的“简史密斯”。

SingleOrDefault() will throw an exception if there is more than one element that matches the p.Name == bidInfo.ArchitectPerson.Name criterion. 如果有多个元素与p.Name == bidInfo.ArchitectPerson.Name标准匹配, SingleOrDefault()将抛出异常。

So long as the "Smith cases" outlined above describe an acceptable state for your program, use FirstOrDefault() . 只要上面概述的“Smith案例”描述了程序的可接受状态,请使用FirstOrDefault()

If I understand correctly, you are trying to find the first company with a person that has the same name as the architect, and then return both. 如果我理解正确,你试图找到第一家公司,其中一个人与建筑师同名,然后返回两者。

If so, then I think this will work: 如果是这样,那么我认为这将有效:

var query =
    from c in bidInfo.Companies
    from p in c.CompanyDomain.People
    where p.Name == bidInfo.ArchitectPerson.Name
    select new { c, p };

var first = query.FirstOrDefault();

Company existingCompany = first?.c;
Person existingPerson = first?.p;

Let me know if I've missed anything. 如果我错过了什么,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM