Linq语法及用法
引用来自:http://blog.sina.com.cn/s/blog_9c47a1520100vjuf.html
System.Linq.IGrouping<T> System.Linq.ILookup<TKey,TElement> System.Linq.IOrderedEnumerable<T> System.Linq.IOrderedQueryable System.Linq.IOrderedQueryable<T> System.Linq.IQueryable System.Linq.IQueryable<T> System.Linq.IQueryProvider 集合扩展方法 集合扩展方法的实现:一个Where的例子
public class a { public int ID { get; set; } public string Name { get; set; } public static void Main() { List<a> list = new List<a> { new a { ID = 1, Name = "wxd" }, new a { ID = 2, Name = "lzm" }, new a { ID = 3, Name = "wxwinter" } }; var l1 = list.WxdWhere(le => le.Name == "lzm"); // 上面的(Lambda表达式)等同于下面的(匿名方法) IEnumerable<a> l2 = list.WxdWhere(delegate(a le) { return le.Name == "lzm"; }); //与调用系统Where一样 var l3 = list.Where(le => le.Name == "lzm"); } } public static class e { // 相关委托 public delegate TResult Func<T, TResult>(T arg); //相关Where扩展方法 //Func<TSource, bool>:接受一个类型为TSource的参数 //Func<TSource, bool>:某个需要满足的条件,返回bool值 public static IEnumerable<TSource> WxdWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) { foreach (TSource item in source) { if (predicate(item)) { yield return item; } } } }
延迟类 1.须引用System.Core 2.对所有的集合者适用 3.最好用var对象存值 4.可以连用
var li = ls.OrderBy(i => i).Take(5); 表示排序后的前5个
5.使用System.Func委托
System.Func<TResult> System.Func<T,TResult> System.Func<T1,T2,TResult> System.Func<T1,T2,T3,TResult> System.Func<T1,T2,T3,T4,TResult>
6.只是视图,不创建复本 Select 选择 与ConvertAll 不一样,该方法以视图形式反回集合,不创建复本
Lambda表达式 List<string> ls = new List<string>() { "1", "2", "3" }; // System.Collections.Generic.IEnumerable<int> li = ls.Select(i => int.Parse(i)); ; var li = ls.Select(i=>int.Parse(i));
标准委托 static void Main(string[] args) { List<string> ls = new List<string>() { "1", "2", "3" }; var li = ls.Select<string,string>(stringAdd); } public static string stringAdd(string s) { return s+"wxd"; }
匿名方法 List<string> ls = new List<string>() { "1", "2", "3" }; var li = ls.Select<string, int>(delegate(string s) { return int.Parse(s); });
Linq同类功能 List<string> ls = new List<string>() { "1", "2", "3" }; var li = from temp in ls select temp;
SelectMany 选择
Lambda表达式 List<string> ls = new List<string>() { "wxd/1", "lzm/2", "wxwinter/3" }; var li = ls.SelectMany(p => p.Split('/')); foreach (var s in li) { Console.WriteLine(s); }
对应Select效果 var ll = ls.Select(p => p.Split('/')); foreach (var s in ll) { foreach (var ss in s) { Console.WriteLine(ss); } }
Where 条件 与FindAll不一样,该方法以视图形式反回集合,不创建复本
List<int> ls = new List<int>() { 1, 2, 3, 4, 5 }; var li = ls.Where(i => i < 3); //li中此时只有1,2 ls[0] = 333; //1已改为333,li中此时只有2
Lambda表达式 //以视图形式,不创建复本 List<int> ls = new List<int>() { 1, 2, 3 ,4,5}; //System.Collections.Generic.IEnumerable<int> li = ls.Where(i => i < 3); var li = ls.Where(i => i < 3);
标准委托 static void Main(string[] args) { List<int> ls = new List<int>() { 1, 2, 3, 4, 5 }; // System.Func<int, bool> ww = new Func<int, bool>(w); // var li = ls.Where(ww); var li = ls.Where(w); } public static bool w(int v) { return v<3; }
匿名方法 List<int> ls = new List<int>() { 1, 2, 3, 4, 5 }; var li = ls.Where(delegate(int v){return v<3;});
Linq同类功能 List<int> ls = new List<int>() { 1, 2, 3, 4, 5 }; var li = from temp in ls where temp< 3 select temp;
OrderBy 排序升 与Sort不一样,以视图形式,不创建复本,不改变原集合的顺序
List<int> ls = new List<int>() { 1, 3, 2, 4, 5, 0, 8, 1 }; var li = ls.OrderBy(i => i); //---- ls[1] = 6666; ls.Add(-5); //---li中的顺序已重新排列
Lambda表达式 //以视图形式,不创建复本 //不改变原集合的顺序 List<int> ls = new List<int>() { 1, 3, 2, 4, 5, 0, 8, 1 }; //System.Collections.Generic.IEnumerable<int> li = ls.OrderBy(i => i); var li = ls.OrderBy(i => i);
标准委托 static void Main(string[] args) { List<int> ls = new List<int>() { 1, 3, 2, 4, 5, 0, 8, 1 }; var li = ls.OrderBy<int,int >(w ); } public static int w(int i) { return -i; //降序 }
匿名方法 List<int> ls = new List<int>() { 1, 3, 2, 4, 5, 0, 8, 1 }; //var li = ls.OrderBy<int, int>(delegate(int i) { return -i; }); var li = ls.OrderBy(delegate(int i) { return -i; })
Linq同类功能 List<int> ls = new List<int>() { 1, 3, 2, 4, 5, 0, 8, 1 }; var li = from temp in ls orderby temp select temp;
OrderByDescending 排序降
匿名方法 List<int> ls = new List<int>() { 1, 3, 2, 4, 5, 0, 8, 1 }; var li = ls.OrderByDescending(delegate(int i) { return i; });
Lambda表达式 List<int> ls = new List<int>() { 1, 3, 2, 4, 5, 0, 8, 1 }; var li = ls.OrderByDescending(i => i);
OrderBy模拟 List<int> ls = new List<int>() { 1, 3, 2, 4, 5, 0, 8, 1 }; var li = ls.OrderBy(delegate(int i) { return -i; });
Linq同类功能 List<int> ls = new List<int>() { 1, 3, 2, 4, 5, 0, 8, 1 }; var li = from temp in ls orderby temp descending select temp;
GroupBy 分组 该方法分组结果集合 System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>>
Lambda表达式 string[] wxd = { "Java", "C#", "C++", "Delphi", "VB.net", "VC.net", "C++ Builder", "Kylix", "Perl", "Python" }; var query = wxd.GroupBy(p => p.Length); foreach (var item in query) { Console.WriteLine("长度 {0}", item.Key); foreach (var val in item) { Console.WriteLine(val); } }
Linq同类功能 string[] wxd = { "Java", "C#", "C++", "Delphi", "VB.net", "VC.net", "C++ Builder", "Kylix", "Perl", "Python" }; var query = from item in wxd group item by item.Length into lengthGroups select lengthGroups;
Join 联合查询
Lambda表达式 static void Main(string[] args) { List<T1> ls1 = new List<T1> { new T1 { Name = "a", Value = 1 }, new T1 { Name = "b", Value = 2 }, new T1 { Name = "c", Value = 3 } }; List<T2> ls2 = new List<T2> { new T2 { ID = "a", bak = "hello" }, new T2 { ID = "a", bak = "wxwinter" }, new T2 { ID = "c", bak = "lzm" }, }; //返回一个新的匿名类的集合 var li=ls1.Join(ls2,temp1=>temp1.Name,temp2=>temp2.ID,(temp1,temp2)=>new { temp1.Name, temp1.Value, temp2.ID, temp2.bak }); foreach (var s in li) { Console.WriteLine("{0}-{1}-{2}-{3}", s.Name, s.ID, s.Value, s.bak); } } } public class T1 { public string Name; public int Value; } public class T2 { public string ID; public string bak; }
Linq同类功能 //返回一个新的匿名类的集合 var li = from temp1 in ls1 join temp2 in ls2 on temp1.Name equals temp2.ID select new { temp1.Name, temp1.Value, temp2.ID, temp2.bak };
GroupJoin
Lambda表达式 static void Main(string[] args) { List<T1> ls1 = new List<T1> { new T1 { Name = "a", Value = 1 }, new T1 { Name = "b", Value = 2 }, new T1 { Name = "c", Value = 3 } }; List<T2> ls2 = new List<T2> { new T2 { ID = "a", bak = "hello" }, new T2 { ID = "a", bak = "wxwinter" }, new T2 { ID = "c", bak = "lzm" }, }; //返回一个新的匿名类的集合 var li = ls1.GroupJoin(ls2, temp1 => temp1.Name, temp2 => temp2.ID, (temp1, temp2) => new { temp1.Name, temp1.Value, 个数 = temp2.Count() }); foreach (var s in li) { Console.WriteLine("Name :{0},共有:{1}",s.Name,s.个数.ToString()); } } } public class T1 { public string Name; public int Value; } public class T2 { public string ID; public string bak; }
Linq同类功能 //返回一个新的匿名类的集合 var li = from temp1 in ls1 join temp2 in ls2 on temp1.Name equals temp2.ID into newtab select new { temp1.Name, temp1.Value, 个数 = newtab.Count() };
Take 获取集合的前n个元素
//以视图形式,不创建复本 List<int> ls = new List<int>() { 1, 3, 2, 4, 5, 0, 8, 1 }; var li = ls.Take(5); //li[1, 3, 2, 4, 5] ls.Insert(0, 999); //li[999,1, 3, 2, 4]
Skip 跳过集合的前n个元素
List<int> ls = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1,50 }; var li = ls.Skip(5);
Distinct 过滤集合中的相同项
List<int> ls = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1 }; var li = ls.Distinct();
Union 连接不同集合,自动过滤相同项
List<int> ls1 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1 }; List<int> ls2 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1,100,200,100,300 }; var li = ls1.Union(ls2);
Concat 连接不同集合,不会自动过滤相同项
List<int> ls1 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1 }; List<int> ls2 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1,100,200,100,300 }; var li = ls1.Concat(ls2);
Intersect 获取不同集合的相同项(交集)
List<int> ls1 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1,50 }; List<int> ls2 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1,100,200,100,300 }; var li = ls1.Intersect(ls2);
Except 从某集合中删除其与另一个集合中相同的项
List<int> ls1 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1,50 }; List<int> ls2 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1,100,200,100,300 }; var li = ls1.Except(ls2);
Reverse 反转集合
List<int> ls = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1,50 }; ls.Reverse();
TakeWhile 条件第一次不成立就跳出循环
List<int> ls = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1,50 }; var li =ls.TakeWhile(p=>p<4);
SkipWhile 条件第一次不成立就失效,将后面的数据全取
List<int> ls = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8,9 ,1,2,3}; var li =ls.SkipWhile(p=>p<=3);
Cast 将集合转换为强类型集合
ArrayList ls1 = new ArrayList {"a","b","1" }; // ArrayList ls1 = new ArrayList { "a", "b", 1 }; //下面的转换会报错 // List<string > ls2 = (List<string >)ls1; //编译会报错 // List<string> ls3 = (List<string>)ls1.Cast<string>(); //运行会报错 var ls4 = ls1.Cast<string >(); List<string> ls5 = ls1.Cast<string>().ToList<string>();
OfType 过滤集合中的指定类型
ArrayList ls1 = new ArrayList {"a","b","c",1 }; var ls4 = ls1.OfType<string>().Cast<string >(); ls1[0] = 345; foreach (object s in ls4) { System.Console.WriteLine(s); }
不延迟(浅复本) Single 集合中符合条件的唯一元素,浅复本 得到的是复本 集合中没有会报错 集合中有两个以上会报错
List<int> ls2 = new List<int>() { 1, 3, 2, 2, 5, 2, 8 }; var v = ls2.Single(p => p==1); //如果集合中没有,或有两个以上会报错,
SingleOrDefault 集合中符合条件的唯一元素(没有则返回类型默认值),浅复本 得到的是复本 集合中没有则返回类型默认值 集合中有两个以上会报错
List<int> ls2 = new List<int>() { 1, 3, 2, 2, 5, 2, 8 }; var v = ls2.SingleOrDefault(p => p == 1); //集合中有两个以上会报错,集合中没有则返回类型默认值
First 集合的第一个元素,浅复本 得到的是复本 集合中没有会报错
List<int> ls = new List<int>() { 1, 3, 2, 2, 5, 2, 8 }; var o = ls.First(); //第一 var o1 = ls.First(p=>p>2); //第一个大于2的
FirstOrDefault 集合中的第一个元素(没有则返回类型默认值),浅复本 得到的是复本 集合中没有则返回类型默认值
List<int> ls = new List<int>() { 1, 3, 2, 2, 5, 2, 8 }; var o = ls.FirstOrDefault(); //第一个 var o1 = ls.FirstOrDefault(p => p > 2); //第一个大于2的
Last 集合中的最后一个元素,浅复本 得到的是复本 集合中没有会报错
List<int> ls = new List<int>() { 1, 3, 2, 2, 5, 2, 8,1 }; var o = ls.Last(); //最后一个 var o1 = ls.Last(p => p > 2); //最后一个大于2的
LastOrDefault 集合中的最后一个元素(没有则返回类型默认值),浅复本 得到的是复本 集合中没有则返回类型默认值
List<int> ls = new List<int>() { 1, 3, 2, 2, 5, 2, 8,1 }; var o = ls.LastOrDefault(); //最后一个 var o1 = ls.LastOrDefault(p => p > 2); //最后一个大于2的
ElementAt 集合中指定索引的元素,浅复本 得到的是复本 集合中没有会报错
List<int> ls = new List<int>() { 1, 3, 2, 2, 5, 2, 8,1 }; var o = ls.ElementAt(1);
ElementAtOrDefault 集合中指定索引的元素(没有则返回类型默认值),浅复本 得到的是复本 集合中没有则返回类型默认值
List<int> ls = new List<int>() { 1, 3, 2, 2, 5, 2, 8,1 }; var o = ls.ElementAtOrDefault(100);
Contains 判断集合中是否包含有某一元素
public class a { static void Main(string[] args) { List<T2> ls2 = new List<T2> { new T2 { ID = "a", bak = "hello" }, new T2 { ID = "a", bak = "wxwinter" }, new T2 { ID = "c", bak = "lzm" }, }; T2 obj = new T2() { bak = "sss", ID = "ok" }; ls2.Add(obj); bool b = ls2.Contains(obj); //true 更多肉容见对像比效 } } public class T2 { public string ID; public string bak; }
Any 判断集合中是否有元素满足某一条件
public class a { static void Main(string[] args) { List<T2> ls2 = new List<T2> { new T2 { ID = "a", bak = "hello" }, new T2 { ID = "a", bak = "wxwinter" }, new T2 { ID = "c", bak = "lzm" }, }; bool b1 = ls2.Any(p => p.bak.Contains("ell") ); //true bool b2 = ls2.Any(p => p.ID=="a" ); //true } } public class T2 { public string ID; public string bak; }
All 判断集合中是否所有元素都满足某一条件
public class a { static void Main(string[] args) { List<T2> ls2 = new List<T2> { new T2 { ID = "a", bak = "hello" }, new T2 { ID = "a", bak = "wxwinter" }, new T2 { ID = "c", bak = "lzm" }, }; bool b1 = ls2.All(p => p.bak.Contains("e") );//false bool b2 = ls2.All(p => p.ID.Length ==1 ); //true } } public class T2 { public string ID; public string bak; }
SequenceEqual 判断两个集合内容是否相同
List<int> ls1 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1 }; List<int> ls2 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1 }; bool b1 = ls2.SequenceEqual(ls1); //true
Count 、LongCount集合中的元素个数
List<int> ls2 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 1 }; int b1 = ls2.Count(p => p < 3); //5 int b2 = ls2.Count(); //8
Average 、Sum集合平均值求和
List<int> ls3 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 2 }; var b1 = ls3.Average(); //3.125 var b2 = ls3.Sum(); //25
static void Main(string[] args) { List<T1> ls1 = new List<T1> { new T1 { Name = "a", Value = 1 }, new T1 { Name = "b", Value = 2 }, new T1 { Name = "c", Value = 4 } }; var b1 = ls1.Average(p=>p.Value); //2.33333333333333 var b2 = ls1.Sum(p=>p.Value); //7 } } public class T1 { public string Name; public int Value; }
Max、Min 集合最大值,最小值
List<int> ls3 = new List<int>() { 1, 3, 2, 2, 5, 2, 8, 2 }; var b1 = ls3.Min(); //1 var b2 = ls3.Max(); //8
public class a { static void Main(string[] args) { List<T1> ls1 = new List<T1> { new T1 { Name = "a", Value = 1 }, new T1 { Name = "b", Value = 2 }, new T1 { Name = "c", Value = 4 } }; var b1 = ls1.Min(p=>p.Value); //1 var b2 = ls1.Max(p=>p.Value); //4 } } public class T1 { public string Name; public int Value; }
Aggregate 根据输入的表达式获取一个聚合值
List<int> ls3 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9,10 }; var b1 = ls3.Aggregate((x, y) => x + y );//55 累加 var b2 = ls3.Aggregate((x, y) => x * y);//3628800 累乘
public class a { static void Main(string[] args) { List<T1> ls1 = new List<T1> { new T1 { Name = "a", Value = 1 }, new T1 { Name = "b", Value = 2 }, new T1 { Name = "c", Value = 4 } }; var b3 = ls1.Aggregate<T1>((x, y) => new T1 { Value = x.Value + y.Value }); System.Console.WriteLine(b3.Value); //7 } } public class T1 { public string Name; public int Value; }
DefaultIfEmpty 查询结果为空则返回默认值,浅复本 如果集合是的无素为空(count==0),就向集合中插入一个默认元素
List<string > ls3 = new List<string >() { "wxd","lzm","wxwinter" ,"wxd"}; var ls4 = ls3.Where(p => p == "ok").DefaultIfEmpty("not"); ; //如果不存在返回给集合一个自定义的对象
ToArray 将集合转换为数组,浅复本
List<string > ls3 = new List<string >() { "wxd","lzm","wxwinter" ,"wxd"}; string[] ss = ls3.ToArray();
ToList 将集合转换为List<T>集合,浅复本
List<string > ls3 = new List<string >() { "wxd","lzm","wxwinter" ,"wxd"}; List<string> ss = ls3.ToList(); ls3[0] = "kk"; //不影响 ss 集合
List<string > ls3 = new List<string >() { "wxd","lzm","wxwinter" ,"wxd"}; var ss = ls3.Select(p=>p); ls3[0] = "kk"; //影响 ss 集合
ToDictionary 将集合转换为<K, V>集合,浅复本
public class a { static void Main(string[] args) { List<T1> ls1 = new List<T1> { new T1 { Name = "a", Value = 1 }, new T1 { Name = "b", Value = 2 }, new T1 { Name = "c", Value = 4 } }; System.Collections.Generic.Dictionary<string, T1> dd = ls1.ToDictionary(p => p.Name); var d = ls1.ToDictionary(p => p.Name); //如果集合中有值相同的Name,将报错 //指定对象为键,原集合中的对象为值 foreach (var s in dd) { System.Console.WriteLine(s); } } } public class T1 { public string Name; public int Value; }
Lambda表达式 Lambda表达式是委托的一种实现方式。与匿名方法类似。 格式
声名参数 => 分隔符 表达式 表达式的结果为委托返回值
p => p + 1 p => p < 4 (int p) => p +1 (p1,p2) => p1 + p2
例1(比效)
Lambda表达式 public delegate TR lzm<T, TR>(T v1); class a { public void f(lzm<int, bool> func) { bool b= func(100); if (b) { System.Console.WriteLine("v大于100"); } else { System.Console.WriteLine("v小于100"); } } } class Program { static void Main(string[] args) { a obj = new a(); int v = 70; obj.f(p => v > p); obj.f((int p) => v > p); //显示类型 } }
委托 public delegate TR lzm<T, TR>(T v1); class a { public void f(lzm<int, bool> func) { bool b= func(100); if (b) { System.Console.WriteLine("v大于100"); } else { System.Console.WriteLine("v小于100"); } } } class Program { static void Main(string[] args) { a obj = new a(); obj.f(w); } public static bool w(int v1) { int v = 70; bool b = v > v1; return b; } }
匿名方法 public delegate TR lzm<T, TR>(T v1); class a { public void f(lzm<int, bool> func) { bool b= func(100); if (b) { System.Console.WriteLine("v大于100"); } else { System.Console.WriteLine("v小于100"); } } } class Program { static void Main(string[] args) { a obj = new a(); obj.f( delegate(int v1) {return 70 > v1; }); Console.Read(); } }
例2(多参)
public delegate T lzm<T1, T2, T>(T1 v1, T2 v2); class a { public void f(lzm<int, int, int> func,int x,int y) { int b = func(x, y); System.Console.WriteLine(b); } } class Program { static void Main(string[] args) { a obj = new a(); obj.f((v1, v2) => v1 - v2,100,200); Console.Read(); } }
例3(list.Where)
public class a { public int ID { get; set; } public string Name { get; set; } public static void Main() { List<a> list = new List<a> { new a { ID = 1, Name = "wxd" }, new a { ID = 2, Name = "lzm" }, new a { ID = 3, Name = "wxwinter" } }; var l1 = list.WxdWhere(le => le.Name == "lzm"); // 上面的(Lambda表达式)等同于下面的(匿名方法) IEnumerable<a> l2 = list.WxdWhere(delegate(a le) { return le.Name == "lzm"; }); //与调用系统Where一样 var l3 = list.Where(le => le.Name == "lzm"); } } public static class e { // 相关委托 public delegate TResult Func<T, TResult>(T arg); //相关Where扩展方法 //Func<TSource, bool>:接受一个类型为TSource的参数 //Func<TSource, bool>:某个需要满足的条件,返回bool值 public static IEnumerable<TSource> WxdWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) { foreach (TSource item in source) { if (predicate(item)) { yield return item; } } } }
Lambda表达式中Lifting
public class a { static void Main(string[] args) { List<Func<int>> list = new List<Func<int>>(); for (int i = 0; i < 3; i++) { //int temp = i; // list.Add(() => temp); // 1,2,3 这个temp,就称为lifting。lift是美语中的电梯,翻译为梯子或垫脚石,比较妥帖 list.Add(() => i); //3,3,3 } foreach (var item in list) { Console.WriteLine(item()); } } }
所盼望输出,0,1,2,而实际结果是3,3,3。 两个原因。 第一,在for循环中,只能有一个 i 变量。即再第一次循环时,i 的地址就分配好了,不会因为循环次数的多少而发生任何改变,其改变的只能是里面装载的值。 第二,lambda表达式在构造时,传进去的是变量的地址,而不是具体值。只有当真正执行这个lambda表达式时,才会去确定它的值。这就是为什么上面的例子中,其结果均为3。(for循环在最后,又给 i 加了1) 在for循环中,定义一临时变量temp存储 i的值即可。因为编译器会对该临时变量重新分配内存,这样,每次循环,都重新分配新的内存,就不会有这个问题
QuerySyntax 查询语法
- 查询句法是使用标准的LINQ查询运算符来表达查询时一个方便的声明式简化写法 每个查询表达式的句法从from子句开始,以select或group子句结束
结果集合 = from 临时变量 in 要查询的集合 [....] select 返回内容; [结果集合]的类型与[select 返回内容]的类型一样 [....] 其它关键字
- 查询句法返回的类型是 System.Collections.IEnumerable 、 System.Collections.Generic.IEnumerable<T>
public class a { static void Main(string[] args) { List<T> ls = new List<T> { new T { Name = "a", Value = 1 }, new T { Name = "b", Value = 2 }, new T { Name = "c", Value = 3 }, }; var l1 = from temp in ls select temp; var l2 = from temp in ls select "姓名:" + temp.Name ; var l3 = from temp in ls where temp.Value <= 2 select temp; System.Collections.IEnumerable l4 = from temp in ls where temp.Value <= 2 select temp; System.Collections.Generic.IEnumerable<T> l5 =from temp in ls where temp.Value <= 2 select temp; } } public class T { public string Name; public int Value; }
3.结果集合以视图方式返回,源集合的内容改变后,会反映到结果集合上。在Select时New全新的对像也是这种情况,因为[结果集合]是动态的,每次访问都按查询句重新生成
public class a { static void Main(string[] args) { List<T> ls = new List<T> { new T { Name = "a", Value = 1 }, new T { Name = "b", Value = 2 }, new T { Name = "c", Value = 3 }, }; var l3 = from temp in ls select new T2 { x=temp.Name , y=temp.Value , z=000}; foreach (var s in l3) { Console.WriteLine(s.x + s.y + s.z); } ls[0].Name = "wxd"; foreach (var s in l3) { Console.WriteLine(s.x + s.y + s.z);} System.Console.Read(); } } public class T { public string Name; public int Value; } public class T2 { public string x; public int y; public int z; }
- 如果不要延迟查询运算,而是要对它们立刻就执行运算, 可以使用内置的ToList() 和ToArray() 运算符来返回一个包括了结果集的List<T>或者数组。
List<int> ls = new List<int>() { 1, 2, 3, 4, 5 }; // var l3 = (from temp in ls select temp); var l3 = (from temp in ls select temp).ToList() ;
from in select
class Program { static void Main(string[] args) { List<T> ls = new List<T> { new T { Name = "a", Value = 1 }, new T { Name = "b", Value = 2 }, new T { Name = "c", Value = 3 }, }; List<int> li = new List<int> {1,2 }; var l1 = from temp1 in ls from temp2 in li where temp1.Value == temp2 select new { temp1.Name, v = temp2 * 100 }; //返回一个新的匿名类的集合 } } public class T { public string Name; public int Value; }
orderby 排序 orderby 字段,字段 descending /ascending
public class a { static void Main(string[] args) { List<T> ls = new List<T> { new T { Name = "a", Value = 1 }, new T { Name = "b", Value = 2 }, new T { Name = "c", Value = 3 }, }; var l1 = (from temp in ls orderby temp.Value, temp.Name select temp); var l2 = (from temp in ls orderby temp.Value descending select temp); var l3 = (from temp in ls where temp.Value < 5 orderby temp.Value ascending select temp); var l4 = (from temp in ls orderby temp.Value ascending where temp.Value < 5 select temp); } } public class T { public string Name; public int Value; }
group by into 分组
分组结果集合 = from 临时变量A in 要查询的集合 group 临时变量A by 分组关键字 into 临时变量B select 临时变量B 其中的 into 关键字表示 将前一个查询的结果视为后续查询的生成器,这里是跟 group by 一起使用的。
分组结果集合 System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>>
public class a { static void Main(string[] args) { List<T> ls = new List<T> { new T { Name = "a", Value = 1 }, new T { Name = "b", Value = 2 }, new T { Name = "c", Value = 3 }, new T { Name = "d", Value = 1 }, new T { Name = "e", Value = 3 } }; var l1 = from temp in ls group temp by temp.Value into gp select gp; var l2 = from temp in ls orderby temp.Name descending //对ls的结果集合排序 group temp by temp.Value into gp orderby gp.Key descending //对组集合排序 select gp; System.Collections.Generic.IEnumerable<System.Linq.IGrouping<int,T>> l3 = from temp in ls group temp by temp.Value into gp select gp; foreach (var s in l2) { Console.WriteLine("{0}组",s.Key); foreach (var ss in s) { Console.WriteLine(ss.Name); } } } } public class T { public string Name; public int Value; }
string[] wxd = { "Java", "C#", "C++", "Delphi", "VB.net", "VC.net", "C++ Builder", "Kylix", "Perl", "Python" }; var query = from item in wxd orderby item group item by item.Length into lengthGroups orderby lengthGroups.Key descending select lengthGroups; foreach (var item in query) { Console.WriteLine("长度 {0}", item.Key); foreach (var val in item) { Console.WriteLine(val); } }
join in on equals 联合查询
public class a { static void Main(string[] args) { List<T1> ls1 = new List<T1> { new T1 { Name = "a", Value = 1 }, new T1 { Name = "b", Value = 2 }, new T1 { Name = "c", Value = 3 } }; List<T2> ls2 = new List<T2> { new T2 { ID = "a", bak = "hello" }, new T2 { ID = "a", bak = "wxwinter" }, new T2 { ID = "c", bak = "lzm" }, }; //返回一个新的匿名类的集合 var li = from temp1 in ls1 join temp2 in ls2 on temp1.Name equals temp2.ID select new { temp1.Name, temp1.Value, temp2.ID, temp2.bak }; foreach (var s in li) { Console.WriteLine("{0}-{1}-{2}-{3}",s.Name,s.ID,s.Value,s.bak); } } } public class T1 { public string Name; public int Value; } public class T2 { public string ID; public string bak; }
into 汇总
static void Main(string[] args) { List<T1> ls1 = new List<T1> { new T1 { Name = "a", Value = 1 }, new T1 { Name = "b", Value = 2 }, new T1 { Name = "c", Value = 3 } }; List<T2> ls2 = new List<T2> { new T2 { ID = "a", bak = "hello" }, new T2 { ID = "a", bak = "wxwinter" }, new T2 { ID = "c", bak = "lzm" }, }; //返回一个新的匿名类的集合 var li = from temp1 in ls1 join temp2 in ls2 on temp1.Name equals temp2.ID into newtab select new { temp1.Name, temp1.Value, 个数 = newtab.Count() }; foreach (var s in li) { Console.WriteLine("Name :{0},共有:{1}",s.Name,s.个数.ToString()); } } } public class T1 { public string Name; public int Value; } public class T2 { public string ID; public string bak; }
DataSource 数据绑定
public class aa { public string a { get; set; } public string b { get; set; } }
winform List<aa> ls = new List<aa> { new aa { a = "a", b = "1" }, new aa { a = "b", b = "2" } }; dataGridView1.DataSource = ls; //数据修改会与集合同步 //两个控件指针会同步 comboBox1.DataSource = ls; comboBox1.DisplayMember = "b";
aspnet protected void Button1_Click(object sender, EventArgs e) { List<aa> ls = new List<aa> { new aa { a = "a", b = "1" }, new aa { a = "b", b = "2" } }; this.GridView1.DataSource = ls; DropDownList1.DataSource = ls; DropDownList1.DataTextField = "b"; DropDownList1.DataValueField = "a"; this.DataBind();