简体   繁体   中英

Select rows based on group by counts

I have a table (Students) with three columns:
StudentID - MotherID - FatherID

I'm having a hard time understanding how I can form a LINQ query to do the following:

I want to get back a list of all students with less than 'y' number of fullsiblings (same mother id and father id) and less than 'z' number of halfsiblings (same father id different mother id).

Using LINQ, I am able to get the correct rows based on half sibling relation ships, but not full sibling relationships:

   var c = studentsDT
                .GroupBy(a => new { a.FatherID}).Where(grp => grp.Count() <= halfSiblings)
                .SelectMany(grp => grp.Select(r => r))
                .GroupBy(a => new { a.MotherID}).Where(grp1 => grp1.Count() <= fullSiblings)
                .SelectMany(grp1 => grp1.Select(r1 => r1));

If table data looked like the following:
1 100 200
2 101 200
3 100 200
4 100 200
5 101 200

In the above data snippet, student 1 has two full siblings and two half siblings by father. Student 2 has one full sibling and three half siblings by father.

If I wanted a list that only had students with no more than two full siblings and no more than 1 half sibling, how could this be achieved?

To get the number of full siblings, you need to specify two keys to group by:

var c = studentsDT
    .GroupBy(a => new { a.FatherID, a.MotherID })
    .Where(g => g.Count() <= fullSiblings)
    .SelectMany(g => g)
    .GroupBy(a => a.FatherID)
    .Where(g => g.Count() <= halfSiblings)
    .SelectMany(g => g);

Note that this counts a full sibling as a half sibling (ie it ensures that the total number of full and half siblings is less than halfSiblings ).

You're going to want a GroupJoin . Something like this:

from student in Students
join sibling in Students
    on student.FatherID equals sibling.FatherID
    into siblings
where
    siblings.Count(s => s.MotherID == student.MotherID) < fullSiblingLimit &&
    siblings.Count(s => s.MotherID != student.MotherID) < halfSiblingLimit
select student

Note that you specified half siblings sharing a father and not a mother.

If your data set is very large, there is room to tweak the query for efficiency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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