Using the Method Syntax
LINQ consists of extensions strategies that ar connected to the IEnumerable <T> interface. These strategies exist in the System.Linqnamespace and are members of the Enumerable static category. If you’ll be able to recall, extension strategies ar special types of strategies that are use to increase pre-existing sorts from the .NET category library or to a category during which you do not have access to the ASCII text file. parenthetically, you’ll be able to add a ToTitleCase() method to the System.String type that you’ll be able to merely decision victimization normal strings to vary their case vogue to title case. Let’s examine Select <TSource, TResult>() method from the System.Linq namespace and appearance at its declaration, you’ll be able to see that it’s connected to the IEnumerable <T> interface.
public static IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source, Func<TSource, TResult> selector)
The first parameter of Associate in Nursing extension methodology determines which sort to increase. it’s preceded by the this keyword followed by the kind to increase Associate in Nursingd an instance name.You can also see that the return type of this method is IEnumerable<T>. This will allow you to nest or chain method calls as you will see later.
I have said in an earlier lesson that calling the LINQ methods directly requires you to use lambda expressions. Although using anonymous methods are okay, lambda expressions are much simpler and shorter and makes your code more readable. I, therefore, assume that you have a good knowledge of lambda expression before going on with this lesson. You will now be presented with another way to query data using LINQ, and that is by using the method syntax which is simply calling the LINQ methods directly.
The .NET Framework contains delegate types that can hold methods with a different number of parameters and different return types. Looking at the definition of the Select() method, the second parameter is a generic delegate with a type of Func<TSource,TResult>. The delegate will be able to accept a method that has one parameter of type TSource and a return type of TResult. For example,Func<string,int> will be able to accept a method that has one string parameter and returns an int. Figure 1 shows you some delegates you can use depending on the number of parameters of the method it will hold.
Delegate | Description |
---|---|
Func<T1, TResult> | Encapsulates a method that has one parameter of type TSource and returns a value of type TResult. |
Func<T1, T2, TResult> | Encapsulates a method that has two parameters T1 and T2 and returns TResult. |
Func<T1,T2,T3,TResult> | Encapsulates a method that has three parameters and returns a value of type TResult. |
Func<T1,T2,T3,T4, TResult> | Encapsulates a method that has four parameters and returns a value of type TResult. |
Figure 1
Based on the table, you can already see the pattern and guess the delegates for methods with higher number of parameters. The delegate with the highest number of parameters available can have 16 parameters. Back on observing the Select() method, the second parameter accepts a reference to a method with one parameter and returns a value. Let’s look at how we can use the Select() method by passing a lambda expression as its parameter.
int[] numbers = { 1, 2, 3, 4, 5 };
var result = numbers.Select(n => n);
foreach(var n in result)
{
Console.Write(n + " ");
}
Example 2
1 2 3 4 5
Note that the first parameter of an extension method is not actually a parameter but is used to indicate which type to extend. Therefore, the second parameter which accepts a lambda expression becomes the only parameter of method Select(). Inside the Select()method, we used a lambda expression that accepts one integer parameter and returns an integer value. Again, if you don’t know lambda expressions then it might look weird to you. What the lambda expression did was to retrieve every number and then add (return the value) to the query result. The code merely queries every number without modifying them. Let’s modify the lambda expression inside the Select() method to do something more useful.
int[] numbers = { 1, 2, 3, 4, 5 };
var result = numbers.Select(n => n + 1);
foreach(var n in result)
{
Console.Write(n + " ");
}
Example 3
2 3 4 5 6
Our lambda expression parameter now retrieves a value from the numbers array, add 1 to its value, and add the new value to the query result.
Most methods from the System.Linq return IEnumerable<T>. This allows you to nest calls of LINQ methods. For example, consider the line of code below (ignore the new methods for now). The important thing is you can see how LINQ method calls can be nested or chained.
var result = numbers.Where(n => n > 3).OrderBy(n => n).Select(n => n);
This lesson only shows how to use the Select() method but there are numerous LINQ methods that we will be looking at in the upcoming lessons. You will also see how to create more exciting results using the Select() method but for now, the important thing is that you know how to use the method syntax for querying data sources using LINQ.
The method syntax is that the possible way the compiler will LINQ queries. The question expression syntax for querying from information sources is simply a layer to change career these extension strategies. maybe contemplate the question expression below:
var result = from p in persons
where p.Age >= 18
order by p.Name
select p.FirstName + " " + p.LastName;
is made an interpretation of at incorporate time into a progression of calls to relating manners by which from the System.Linq namespace. the specific code utilizes the procedure sentence structure as demonstrated as follows:
var result = persons.Where(p => p.Age >= 18)
.OrderBy(p => p.Name)
.Select(p => p.FirstName + " " + p.LastName);
You can see the chained method calls (I only aligned them for better readability). This was made possible because most of the LINQ methods return IEnumerable<T>. After calling the Where() method, we used the OrderBy() method on the returned data, and then we used the Select() method on the returned data of OrderBy().
Now that you simply apprehend 2 ways that to question information victimization LINQ, question expressions and methodology syntax; the question is that one do you have to use? i like to recommend victimization question expressions as a result of it promotes a declarative type of programming and it’s easier and easier to browse than methodology syntax. however it’s conjointly vital that you simply have information of victimization methodology syntax as a result of this is often the approach CLR browse your question expression.