How would you do a “not in” query with LINQ?

You want the Except operator. var answer = list1.Except(list2); Better explanation here: https://docs.microsoft.com/archive/blogs/charlie/linq-farm-more-on-set-operators NOTE: This technique works best for primitive types only, since you have to implement an IEqualityComparer to use the Except method with complex types.

Using LINQ to concatenate strings

This answer shows usage of LINQ (Aggregate) as requested in the question and is not intended for everyday use. Because this does not use a StringBuilder it will have horrible performance for very long sequences. For regular code use String.Join as shown in the other answer Use aggregate queries like this: string[] words = { … Read more

Preserving order with LINQ

I examined the methods of System.Linq.Enumerable, discarding any that returned non-IEnumerable results. I checked the remarks of each to determine how the order of the result would differ from order of the source. Preserves Order Absolutely. You can map a source element by index to a result element AsEnumerable Cast Concat Select ToArray ToList Preserves … Read more

C# Linq Group By on multiple columns [duplicate]

var consolidatedChildren = from c in children group c by new { c.School, c.Friend, c.FavoriteColor, } into gcs select new ConsolidatedChild() { School = gcs.Key.School, Friend = gcs.Key.Friend, FavoriteColor = gcs.Key.FavoriteColor, Children = gcs.ToList(), }; var consolidatedChildren = children .GroupBy(c => new { c.School, c.Friend, c.FavoriteColor, }) .Select(gcs => new ConsolidatedChild() { School = gcs.Key.School, … Read more

How to get index using LINQ? [duplicate]

myCars.Select((v, i) => new {car = v, index = i}).First(myCondition).index; or the slightly shorter myCars.Select((car, index) => new {car, index}).First(myCondition).index; or the slightly shorter shorter myCars.Select((car, index) => (car, index)).First(myCondition).index;