Browse by Tags

All Tags » C# » Lambda (RSS)
ConvertAll is one of those methods that is not used very often, but when you need it, it is very useful. It converts all of the elements of one list or array into element of another type. In this example the user enters a list of numbers into a TextBox...
5 comment(s)
Filed under: , , , ,
This prior post demonstrated how to use delegates to associated code with a Tag in a WinForms control. This post shows how to accomplish the same thing using Lambda expressions to define the delegate. To try out this example, create a WinForms application...
In this prior post , I demonstrated how to find a specific item in a generic list of items. But what if the item you want to find is in a child list? For example, a list of customers where each customer is a company that has a company name and a list...
2 comment(s)
Filed under: , , , ,
When working with data, you often have values that are null. For example, you may allow entry of a user's birthday, but not require entry. So some of your rows may contain a null date. Or your application may track a customer's last order date...
If you are doing any type of statistical analysis, you probably need to calculate mean, median and mode. There are lots of places on the Web you can find the calculations. This post is different than most in that it uses LINQ and Lambda expressions. Mean...
5 comment(s)
Filed under: , , , , ,
Most of the cool extension methods that you can use with Lambda expressions, such as Where, FirstOrDefault, and Zip, are extensions of the generic IEnumerable<T> class. Interestingly, the .NET multi-dimensional array does not implement the generic...
1 comment(s)
Filed under: , , , ,
One of the new features in C# 4 and VB 10 (.NET 4.0) is the Zip extension method on IEnumerable. This method allows you to merge two lists such as two arrays or two generic List<T>. The Zip extension method merges the two sequences by matching up...
with no comments
Filed under: , , , ,
There are often cases where you need to group on multiple properties and potentially sum on others. This post details how to group and sum on multiple properties using lambda expressions. This example uses an Invoice class, since that provides many opportunities...
11 comment(s)
Filed under: , , , , ,
Someone recently posted in the forums the need to build a " Word Search " puzzle and wanted to generate a list of random letters. .NET has all of the features you need to do this in just two lines of code. In C#: Random rand  = new Random...
7 comment(s)
Filed under: , , , ,
If you have a set of business objects that are related to another set of business objects by a foreign key, you can use the Join method in a Lambda expression to join the two sets of business objects into one. You can then use the resulting set as a single...
9 comment(s)
Filed under: , , , ,
If you retrieve data into a DataTable, it is easy to bind it to a ComboBox. And before Linq, you would filter the DataTable using a DataView. But with Linq, you have some easy to use features for filtering the contents of your ComboBox. First, here is...
A common requirement with an XML file is to find a particular node. If you are targeting the .NET framework 3.5 or higher, you can find the node using Linq to XML or by using Lambda expressions. As with many of my prior XML examples, the XML string is...
3 comment(s)
Filed under: , , , , , ,
Lambda expressions can be assigned to a delegate variable. The lambda expression is then executed when the delegate is called. [To begin with an overview of lambda expressions, start here .] In the following example, a lambda expression is assigned to...
A Func delegate encapsulates a method that returns a value. It takes up to four parameters (and this number is increased in .NET 4.0) plus the return value. [To begin with an overview of lambda expressions, start here .] The following signature is a Func...
Just as the name implies, an action delegate encapsulates a method that performs an action and has no return value. It takes up to four parameters (and this number is increased in .NET 4.0). [To begin with an overview of lambda expressions, start here...
According to Wikipedia , a predicate is “a function which returns a Boolean value”. [To begin with an overview of lambda expressions, start here .] A predicate delegate encapsulates a method that evaluates to True or False. It takes a single parameter...
This post covers the syntax for lambda expressions. It uses the customer list defined in this prior post . [To begin with an overview of lambda expressions, start here .] The first example below uses the FirstOrDefault extension method on the Enumerable...
1 comment(s)
Filed under: , , , ,
A delegate is an object that holds a reference to a method with a particular parameter list and return type. It is basically like an object-oriented, type-safe function pointer. Delegates basically make it possible to treat methods as entities. You can...
In this prior post , I detailed how to build lists of things using a generic List<T>. Once you have a list, you may need to find items in the list. There are several ways to accomplish this, but using lambda expressions is the most concise. [To...
My presentation at our local Code Camp was fun. I covered a lot of material and had a GREAT audience. Several people have asked for the slide information in my blog. So this is the start of several posts on lambda expressions. In addition, I wrote an...
More Posts Next page »