【Linq】-Related Technology

This blog post details an important feature introduced in .NET 3.5: Language Integrated Query (LINQ). With LINQ, we can use the same API to manipulate different data sources. Next let's see what is LINQ and how to use it?

Before that, you need to know the related technologies

1. Implicit types, anonymous types, object initializers

1) Implicit type, created with the var keyword, the C# compiler infers the data type of the variable based on the initial value used to initialize the local variable. (However, in my personal opinion, try not to use the var keyword where a concrete type can be used, because this will make you forget the return value type of the "encapsulated class library" method - loss of readability)
Implicit type usage restrictions:
a ) Implicit typing can only be applied to declarations of local variables within methods or properties, and var cannot be used to define return values, parameter types, or data members of types.
b) Local variables declared with var must be assigned an initial value, and cannot use null as an initial value.
2) An anonymous type is just a class without a name that inherits Object. The C# compiler automatically generates classes with unique names at compile time.
3) Object initializers, which provide a very concise way to create objects and assign values ​​to their properties. (also related to "collection initializers")

Because of C#'s strongly typed language, that is, we must specify the specific type of the variable when declaring it. So when creating anonymous objects, you need to combine implicit types, anonymous types, and object initializers to create anonymous objects. (avoiding casts)
Example:
var person = new { name = "heyuquan" , age = 24 }

2, Lambda expression, Func delegate

1) Lambda expressions are just a simpler way to write anonymous methods, which completely simplifies the use of .NET delegate types.
Lambda expressions are written in C# as "arg-list => expr-body". The left side of the "=>" symbol is the parameter list of the expression, and the right side is the expression body (body). The parameter list can contain 0 or more parameters, separated by commas.
2) Func delegate
Func delegate is a common delegate predefined by Microsoft for us, which encapsulates a method that has: zero or more input parameters of a specified type and returns a result value of a specified type.
write picture description here
Example:

static void Main(string[] args)
    {
        // 委托函数
        Func<string, string, string> func1 = Hello;
        // 匿名方法
        Func<string, string, string> func2 =
            delegate(string a, string b) 
            { 
                return "欢迎光临我的博客" + Environment.NewLine + a + " " + b; 
            };
        // Lambda表达式
        Func<string, string, string> func3 =
            (a, b) => { return "欢迎光临我的博客" + Environment.NewLine + a + " " + b; };
        // 调用Func委托
        string helloStr = func2("滴答的雨", @"http://www.cnblogs.com/heyuquan/");
        Console.WriteLine(helloStr);
}
    static string Hello(string a, string b)
    {
        return "欢迎光临我的博客" + Environment.NewLine + a + " " + b; 
    }

3. Extension method

1) The extension method is declared in a static class and is defined as a static method whose first parameter needs to be identified with the this keyword, indicating the type it extends.
2) An extension method can write a method into a class that did not initially provide the method. You can also add methods to any class that implements an interface, so that multiple classes can use the same implementation code. (In LINQ, System.Linq.Queryable.cs and System.Linq.Enumerable.cs add extension methods to the interface)
3) Although the extension method is defined as a static method, it is not necessary to provide the class name that defines the static method when it is called , just introduce the corresponding namespace, and the access method is the same as the instance method.
4) An extension method cannot access private members of the type it extends.

Example:

public static IEnumerable<TSource> MyWhere<TSource>(
    this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
    foreach (TSource item in source)
    {
        if (predicate(item))
            yield return item;
    }
}

Reprinted from: http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856176&siteId=291194637