LINQ

static void QueryOverStrings()
                {
                        //字符串数组
                        string[] currentGames = { "Morrowind", "BioShock", "Half Life 2:Episode 1", "The Darkness", "Daxer", "System Shock 2" };
                        
                        //建造一个查询表达式,代表数组中多于6个字母的项
                        var subset = from g in currentGames
                                                                                 where g.Length > 6
                                                                                 orderby g
                                                                                 select g;

                        foreach (var s in subset)
                        {
                                Console.WriteLine("Item:{0}", s);
                        }
                        
                        
                        Console.ReadLine();
                }
 
 
通常使用隐式类型来捕获LINQ的结果集。
 
 
延迟执行,可以为相同的容器多次应用相同的LINQ查询,始终获得最新的结果。
static void QueryOverInts()
                {
                        int[] numbers = { 10, 20, 30, 40, 1, 2, 3, 8 };

                        //获取小于10的数
                        var subset = from i in numbers
                                                 where i < 10
                                                 select i;

                        foreach (int j in subset)
                                Console.WriteLine(j);
                        Console.WriteLine();

                        numbers[1] = 3;
                        foreach (int j in subset)
                                Console.WriteLine(j);
                        Console.WriteLine();
                }
 
 
可以使用Enumberable获取查询的总数
如:
int numb = (from g in currentGames
                         where g.length>6
                         order by g
                         select g).Count<string>();
 
排除重复对象:
var makes = (from c in Mycars select c.make).Distinct<string>();
使用Enumerable类型的泛型Reverse<T>()
 
对表达式进行排序使用orderby,默认是升序排序,使用descending表示降序排序
 
使用Enumberable.Except()方法可以获得两个容器之间的不同。
 
 
将查询结构转换为Array类型。
var makeColors = from c in myCars select new {c.make,c.color };
return makesColors.ToArray();
 
从Main()中调用和处理数据:
Array objs=GetPro();
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章