How to Convert a Collection From One Type to Another Type Using LINQ Cast

Typically we have the problem of having a collection of objects which we would like to convert into another type of collection (for example, from an ArrayList to a List<>). If the collection inherits from IENumerable or IQueryable (List<>  for example), we can use LINQ's ConvertAll. However, if the collection instead inherits from System.Collections (ArrayList [...]

Share

Read Users' Comments (0)

How to Find the Average Value of a List Using LINQ Average

Often times when dealing with large lists of numbers (or strings), we need to find the average value of the list. Maybe we are calculating a student's GPA, or trying find the average age of visitors to a website. In the 2.0 world, we had to create a loop (typically foreach), count all the values [...]

Share

Read Users' Comments (0)

Testing if Any Element in a C# List Meets a Condition

A common problem we developers have to deal with is determining if one or more items in a list meets a condition. We don't care if the whole list meets the condition, so long as at least one item meets the condition. For example, let's say you have a list of animals and their vaccination [...]

Share

Read Users' Comments (0)

Testing an Expression in a C# List using LINQ ALL

Let's say you have  list of items, and you need to quickly find out if all the items in the list match (or don't match) a given criteria. Maybe you want to know if a list of ints are all even, or if all items in the list start with the letter P. I write [...]

Share

Read Users' Comments (0)

Using LINQ to Convert an IENumerable to a List

I see a lot of examples for LINQ where a foreach loop is used after the select to populate the result list. There are situations where you would want to use the foreach loop, mainly to take advantage of LINQ's deferred execution. However, most developers will not need, nor desire, to have deferred execute, especially [...]

Share

Read Users' Comments (0)

How to Use LINQ Aggregate to on a C# List

Hello, and welcome to the first blog post on LINQ Exchange. My goal for this blog is to cover each LINQ extension, describe it, and show examples of how and why you might use it. I will probably be going in alphabetical order, at least in the beginning. If you have a request for a [...]

Share

Read Users' Comments (0)