LEFT OUTER JOIN in LINQ

As stated in “Perform left outer joins”: var q = from c in categories join p in products on c.Category equals p.Category into ps from p in ps.DefaultIfEmpty() select new { Category = c, ProductName = p == null ? “(No products)” : p.ProductName };

Use LINQ to get items in one List, that are not in another List

This can be addressed using the following LINQ expression: var result = peopleList2.Where(p => !peopleList1.Any(p2 => p2.ID == p.ID)); An alternate way of expressing this via LINQ, which some developers find more readable: var result = peopleList2.Where(p => peopleList1.All(p2 => p2.ID != p.ID)); Warning: As noted in the comments, these approaches mandate an O(n*m) operation. … Read more

Which method performs better: .Any() vs .Count() > 0?

If you are starting with something that has a .Length or .Count (such as ICollection<T>, IList<T>, List<T>, etc) – then this will be the fastest option, since it doesn’t need to go through the GetEnumerator()/MoveNext()/Dispose() sequence required by Any() to check for a non-empty IEnumerable<T> sequence. For just IEnumerable<T>, then Any() will generally be quicker, … Read more

Dynamic LINQ OrderBy on IEnumerable / IQueryable

Just stumbled into this oldie… To do this without the dynamic LINQ library, you just need the code as below. This covers most common scenarios including nested properties. To get it working with IEnumerable<T> you could add some wrapper methods that go via AsQueryable – but the code below is the core Expression logic needed. … Read more

Using LINQ to remove elements from a List

Well, it would be easier to exclude them in the first place: authorsList = authorsList.Where(x => x.FirstName != “Bob”).ToList(); However, that would just change the value of authorsList instead of removing the authors from the previous collection. Alternatively, you can use RemoveAll: authorsList.RemoveAll(x => x.FirstName == “Bob”); If you really need to do it based … Read more

LINQ Aggregate algorithm explained

The easiest-to-understand definition of Aggregate is that it performs an operation on each element of the list taking into account the operations that have gone before. That is to say it performs the action on the first and second element and carries the result forward. Then it operates on the previous result and the third … Read more

IEnumerable vs List – What to Use? How do they work?

IEnumerable describes behavior, while List is an implementation of that behavior. When you use IEnumerable, you give the compiler a chance to defer work until later, possibly optimizing along the way. If you use ToList() you force the compiler to reify the results right away. Whenever I’m “stacking” LINQ expressions, I use IEnumerable, because by … Read more

LINQ equivalent of foreach for IEnumerable

There is no ForEach extension for IEnumerable; only for List<T>. So you could do items.ToList().ForEach(i => i.DoStuff()); Alternatively, write your own ForEach extension method: public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action) { foreach(T item in enumeration) { action(item); } }

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş