简体   繁体   English

在 linq 查询 c# 中使用条件语句

[英]Using conditional statements in linq query c#

I have 2 queries.我有 2 个查询。 First gets the full name of a committee head if the committee has more that 1 member.如果委员会有超过 1 名成员,则首先获取委员会负责人的全名。

var result = await db.ExpertCommittees
    .Where(f => f.Id == committeeId)
    .Where(f => f.ExpertCommitteeMembers.Count > 1)
    .Select(f => f.ExpertCommitteeMembers
    .Where(m => m.IsCommitteeHead)
    .FirstOrDefault().Expert.FullName)
    .FirstOrDefaultAsync();

The second one gets the full name of the only committee member if the committee has only 1 member如果委员会只有 1 名成员,则第二个将获得唯一一名委员会成员的全名

var result2 = await db.ExpertCommittees
    .Where(f => f.Id == committeeId)
    .Where(f => f.ExpertCommitteeMembers.Count == 1)
    .Select(f => f.ExpertCommitteeMembers
    .FirstOrDefault().Expert.FullName)
    .FirstOrDefaultAsync();

Is it possible to check how many members does the committee have and then return the correct name all in the same query?是否可以检查委员会有多少成员,然后在同一个查询中返回正确的姓名? Or do I first have to check how many members does the committee have and then run the appropriate query seperatly?还是我必须先检查委员会有多少成员,然后单独运行适当的查询?

If I understand correctly you can try to let condition into inner linq where如果我理解正确,您可以尝试将条件放入内部 linq where

var result = await db.ExpertCommittees
    .Where(f => f.Id == committeeId)
    .Select(f => f.ExpertCommitteeMembers.Where(m => 
                 (m.IsCommitteeHead && 
                 f.ExpertCommitteeMembers.Count > 1)||f.ExpertCommitteeMembers.Count == 1).FirstOrDefault().Expert.FullName)
    .FirstOrDefaultAsync();

you can Create a create anonymous object in linq query select.您可以在 linq 查询选择中创建一个创建匿名对象。 which will contain FullName and count.它将包含全名和计数。

var result = await db.ExpertCommittees
    .Where(f => f.Id == committeeId)
    .Where(f => f.ExpertCommitteeMembers.Count > 1)
    .Select(f => new
    {
        FullName = f.ExpertCommitteeMembers
    .Where(m => m.IsCommitteeHead)
    .FirstOrDefault().Expert.FullName,
        Count = f.ExpertCommitteeMembers.Count
    })
    .FirstOrDefaultAsync();

A generated Join, as it is generated from ExpertCommittees to ExpertCommitteeMembers, with a Navigation-Collection, will always do a Left Join , what you want is an Inner Join .生成的 Join,因为它是从 ExpertCommittees 到 ExpertCommitteeMembers 生成的,带有导航集合,将始终执行Left Join ,您想要的是Inner Join It will give you only items with entities in both tables.它只会为您提供两个表中都有实体的项目。

This will be something like这将是类似

    db.ExpertCommittees.Join(db.ExpertCommittemember, x=>someid, y=>somid, 
          (comittee, member) => new { comittee, member});

But this will give you one line per member.... with the possibility to filter by "IsComitteeHead" or whatever.但这将为每个成员提供一行……可以通过“IsComitteeHead”或其他方式进行过滤。

You can append Grouping, or use a GroupJoin directly, to have a List of "Commitees", each with a List of it's members ...and only if there are members.您可以附加 Grouping 或直接使用 GroupJoin 来拥有一个“委员会”列表,每个委员会都有一个成员列表……并且仅当有成员时。

Without knowing the data structure, ID's, Foreign-Keys and table name, we cannot produce a valid query.在不知道数据结构、ID、外键和表名的情况下,我们无法生成有效的查询。

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

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