I have a list of objects, can be of any type T
.
How to select a list of objects that appear in that list only once using linq? For example, if my list is
{2,3,4,5,8,2,3,5,4,2,3,4,6}
, then the output should be {6,8}
.
You could try this:
int[] arr = { 2, 3, 4, 5, 8, 2, 3, 5, 4, 2, 3, 4, 6 };
var q =
from g in arr.GroupBy(x => x)
where g.Count() == 1
select g.First();
Use the Count() function.
int[] a = {2,3,4,5,8,2,3,5,4,2,3,4,6};
var selection = from i in a
where (a.Count(n => n == i) == 1)
select i;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With