LINQ系列:C#中与LINQ相关特性

1. 匿名类型

  通过关键字var定义匿名类型,编译器将根据运算符右侧表达式的值来发出一个强类型。

  使用匿名类型时要遵守的一些基本规则:

  ◊ 匿名类型必须有一个初始化值,而且这个值不能是空值(null),因为类型是根据初始化器推断出来的;

  ◊ 匿名类型可以用于简单类型,也可以用于复杂类型。用于定义简单类型时,其价值不大。复合匿名类型需要有成员声明;

  ◊ 匿名类型不能用于类的字段;

  ◊ 匿名类型可以在for循环中用作初始化器;

  ◊ 可以使用new关键字;数组的初始化器必须使用new关键字;

  ◊ 匿名类型可以用于数组;

  ◊ 所有匿名类型都派生自Object类型;

var title = "LINQ to Object";

1.1 复合匿名类型

var product = new { Title = "LINQ to Object", UnitPrice = 10m };

1.2 在for/foreach语句中使用匿名类型

var fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
for (var i = 0; i < fibonacci.Length; i++)
{
    Console.WriteLine(fibonacci[i]);
}
var fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
foreach (var number in from f in fibonacci where f > 5 select f)
{
    Console.WriteLine(number);
}

1.3 匿名类型与using

using( var connection = new SqlConnection(connectionString))
{
    connection.Open();
    // ......
}

2. 数组初始化器

  使用关键字new初始化数组。

var fibonacci = new int[]{ 1, 1, 2, 3, 5, 8, 13, 21 };

3. 匿名泛型方法

Func<long, long> Factorial = delegate(long n)
{
    if (n == 1)
    {
        return 1;
    }

    long result = 1;
    for (int i = 2; i <= n; i++)
    {
        result *= i;
    }
    return result;
};

Console.WriteLine(Factorial(6));
// using System.Diagnostics;
Func<long, long> Factorial = delegate(long n)
{
    return n > 1 ? n * (long)(new StackTrace()
        .GetFrame(0)
        .GetMethod()
        .Invoke(null, new object[] { n - 1 }))
        : n;
};

Console.WriteLine(Factorial(6));

4. 自动实现属性

public string ProductName { get; set; }

  设置只读属性:

public string ProductName { get; private set; }

5.对象初始化器和集合初始化器

  对象初始化器:在不调用类的构造函数以声明性方式创建一个新的对象,并设置该对象的各个属性的值。

Product product = new Product
{
    ProductID = 1,
    ProductName = "LINQ to Object",
    UnitPrice = 10m
};

  集合初始化器用来初始化一个集合。

List<int> num = new List<int> { 0, 1, 2, 6, 7, 8, 9

  结合对象初始化器使用:

List<Product> products = new List<Product> { 
    new Product { ProductID = 1, ProductName = "LINQ to Object", UnitPrice = 10m },
    new Product { ProductID = 2, ProductName = "LINQ to ADO.NET", UnitPrice = 20m }
};

6.  Lambda表达式

  Lambda表达式是一个匿名函数,它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型。

  Lambda表达式的基本形式:

(input parameters) => expression

  其中,input parameters表示输入参数,expression表示表达式。输入参数的数量可以为空,1个或多个。

  当输入参数为空是,Lambda表达式左边的()不能省略。

() => Console.WriteLine("Empty");
Func<DateTime> getDateTime = () => DateTime.Now;

  当Lambda表达式的输入参数的数量为1时,输入参数的()可以省略。

x => x * x;

  当Lambda表达式的输入参数的数量大于1时,输入参数的()是必须的,且参数之间使用逗号分隔。

(x, y) => x * y;

  Lambda的delegate形式转换:

delegate(int x) { return x * x; };

7. 查询表达式

var products = from p in ctx.Products
               where p.UnitPrice > 10m
               select p;

8. 扩展方法

  扩展方法是C#中一个重要的特性,其对于LINQ的实现起着关键的作用。扩展方法在使用上类似于静态方法,但在本质上其是实例方法。

  扩展方法在参数前加上this关键字,参数的类似表示这个扩展方法要对这个类型进行扩展。

public static class Extensions
{
    public static bool IsValidEmailAddress(this string value)
    {
        Regex regex = new Regex(@"^[w.]+@([w]+.)+[w]{2,4}$"return regex.IsMatch(value);
    }
}

关联随笔:C# LINQ需求实现演化

原文地址:https://www.cnblogs.com/libingql/p/4038888.html