Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write this into linq to object query?

Tags:

c#

.net

vb.net

linq

from a list, which got 3 attributes I want to return a new list of that class where attribut1 recurrence in the list equals X

for an example this;

1,a,b
1,c,d
1,e,f
2,a,b
2,c,d
3,a,b
3,c,d
3,e,f
4,a,b
5,a,b
5,c,d
5,e,f
6,a,b
6,e,f

where X = 1 would return that list

4,a,b

where X = 2 would return that list

2,a,b
2,c,d
6,a,b
6,e,f

where X = 3 would return that list

1,a,b
1,c,d
1,e,f
3,a,b
3,c,d
3,e,f
5,a,b
5,c,d
5,e,f

like image 920
Fredou Avatar asked Nov 28 '25 08:11

Fredou


1 Answers

Perfect case for grouping!

var groups = list.GroupBy(s => s.Attribute1);
var recur_1 = groups.Where(g => g.Count() == 1).SelectMany(s => s);
var recur_2 = groups.Where(g => g.Count() == 2).SelectMany(s => s);
var recur_3 = groups.Where(g => g.Count() == 3).SelectMany(s => s);
like image 88
sidney.andrews Avatar answered Nov 29 '25 22:11

sidney.andrews



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!