jeudi 9 janvier 2020

Using conditional statements in linq query c#

I have 2 queries. First gets the full name of a committee head if the committee has more that 1 member.

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

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?

Aucun commentaire:

Enregistrer un commentaire