Using LINQ, can I get a list of all int elements that only occur once?
For instance
{1,2,4,8,6,3,4,8,8,2}
would become
Various Extension Methods you could use:
public static IEnumerable WhereUnique(this IEnumerable items)
{
return items.GroupBy(x => x).Where(x => x.Count() ==1).Select(x => x.First());
}
possibly slightly more performant, depending on the distribution of your data:
public static IEnumerable WhereUnique(this IEnumerable items)
{
return items.GroupBy(x => x).Where(x => !x.Skip(1).Any()).Select(x => x.First());
}
And WhereUniqueBy, which works similiar to MoreLinqs DistinctBy():
public static IEnumerable WhereUniqueBy(this IEnumerable items, Func func)
{
return items.GroupBy(func).Where(x => x.Count() ==1).Select(x => x.First());
}