LINQ to Object

LinQ to Object是指对随意IEnumerableIenumerable<T>集合使用linq查询.它可取代查询不论什么可枚举的集合.List<T>,ArrayDictionary<K,V>. 


LINQ的长处


  和foreache相比有三个长处

  • 它们更简明、更易读,尤其在筛选多个条件时。
  • 它们使用最少的应用程序代码提供强大的筛选、排序和分组功能。
  • 无需改动或仅仅需做非常小的改动就可以将它们移植到其它数据源。


LINQ特性



对象初始化器,这个事实上我们一直在用.就如我们加入非常多的sql參数

SqlParameter[]parameters = {
newSqlParameter("@QuestionID",Convert.ToInt32 (map["QuestionID"])),
newSqlParameter("@ChapterID",map["ChapterID"].ToString () ),
newSqlParameter("@QuestionTypeID",map["QuestionTypeID"].ToString() ),
newSqlParameter("@Remark", map["Remark"].ToString () )};

以上是我们经常使用的一种数组初始化器

以下是集合初始化器.

IList<Book> books = new List<Book> { 
     new Book { Title = "Inside COM", ISBN = "123-456-789",Price=20 },
     new Book { Title = "Inside C#", ISBN = "123-356-d89",Price=100 },
     new Book { Title = "Linq", ISBN = "123-d56-d89", Price = 120 }
};

上节说道LINQ的查询方式有两种,表达式和操作符,Object的操作也是这两种方式;是要明确并非全部的查询和操作符号都具有延时性.我们就開始吧


标准查询whereselect


实例数组的查询

Select[]greetings={
"hello","hello LINQ","How are you"
} 
Var items= from group in greetings where group.lenght>10 select group
//where,selectkeyword是linq to object编程接口标准查询符。grou是查询变量

对集合的查询

//定义一个序列,使用泛型接口时类型T要明白指定详细类型

String[]strArrary={"one","two","three","four","five","six","seven","eight","nine","ten"}
 
//使用标准查询操作符号where,
 
IEnumerable<String> strSquence=strArrary.Where(p=>p.StartWith("t"));
 
//便利输出满足条件的元素
 
Foreach(stringitem in items)
Consel.writeline(item);
Console.Read();


where操作符号返回一个泛型对象。也就是一个序列对象。该对象是在foreach序列化时调用where操作符运行的。这样的查询是延迟查询。

 

比如能够指定一个查询,多次理解查询结果,当被查询的数据在多次力矩之间发生变化,多次查询结果是不同的。每次都是最新的数据

 static void Main(string[] args)
        {
            int[] intArray = new int[] { 9, 8,7 };
            //以下的查询表达式等价于代码 IEnumerable<int> ints = intArray.Select(i => i);
            var ints = from i in intArray
                       select i;
 
            //第一次列举
            foreach (int i in ints)
                Console.WriteLine(i);
 
            //改动元素的值
            intArray[0] = 999;
            intArray[1] = 888;
            intArray[2] = 777;
           Console.WriteLine("---------");
            //第二次列举
            foreach (int i in ints)
                Console.WriteLine(i);
            Console.Read();
        }
 



非延时

假设不想延迟查询,拿上运行,能够再上面的调用方法上转换,ToArrayToLIst等方法

int[] intArray = newint[] { 9, 8, 7 };
            //使用ToArray方法进行转换
            IEnumerable<int> ints =intArray.Select(i => i).ToArray();
 
            foreach (int i in ints)    //第一次列举
                Console.WriteLine(i);
            intArray[0] = 999;   //改动元素的值
            intArray[1] = 888;
            intArray[2] = 777;
           Console.WriteLine("---------");
 
            foreach (int i in ints)  //第二次列举
                Console.WriteLine(i);



为何是同样的,原因在于查询表达式时调用了ToArray方法,马上运行,并将查询结果保存在整数类型数组ints中。两次列举数组中的元素,输出结果同样,改动的仅仅是IntArray数组中的值,并没有影响ints数组值。非常好的一个应用吧


总结


以上述的仅仅是object的冰上一角。很多其它的object的操作方法是在对web页面数据绑定以及form窗口的数据绑定以及综合查询和排序等,以及funt<T,TResult>的应用。下篇介绍泛型托付FuncT<>的应用。

原文地址:https://www.cnblogs.com/yxwkf/p/4077869.html