C#: LINQ – Set Using Custom Method

Please see my other Web articles.

Using a Custom Method during a set operation

In this article I’ll demonstrate how to call a custom method during a LINQ query.

In my previous article, I demonstrated how to use the SET operator during a query.

In this example, I’ll perform another set task, but this time using a custom method.

As you can see, I’ll getting a List<> collection of person objects and performing a set on each FNameLen property to display the length of each person’s first name.

In addition, I’m getting a new collection of cars.

Now, I want to perform a similar set, but this time on the cars collection, and instead of a helper method, I want to call a custom method.

In this method, I’m using the LINQ Where() method to isolate those car objects that below to the owner (userID) passed into the method. Then, I’m calling Count() to return that person’s number of cars they own.

Using the method syntax for LINQ, I can now perform another set operation to update each person’s CarCount property by calling my custom GetCarCountByOwner() method.