4 System.Linq    10

4.1 System.Linq.Enumerable    10

4.2 System.Linq.Queryable    10

4.3 System.Linq.Lookup <TKey,TElement>    10

4.4 System.Linq.Expressions.Expression    10

5 接口    10

5.1 IEnumerable 、IEnumerator    10

5.1.1 正常使用    10

5.1.2 C#的 yield    12

5.2 IEnumerable <T>    12

5.3 IEnumerator <T>    12

5.4 ICollection    12

5.5 ICollection <T>    13

5.6 IList    13

5.7 IList <T>    13

5.8 IEqualityComparer    13

5.9 IEqualityComparer <T>    13

5.10 IDictionary    13

5.11 IDictionary <TKey,TValue>    13

5.12 IDictionaryEnumerator    13

5.13 IComparer    13

5.13.1 接口方法说明 int Compare(object x, object y)    13

5.13.2 ArrayList.Sort (IComparer) 方法    13

5.14 IComparer <T>    14

5.14.1 接口方法override int Compare(T x, T y)说明    14

5.14.2 List.Sort (IComparer) 方法    14

5.15 System.Linq.IGrouping<T>    14

5.16 System.Linq.ILookup<TKey,TElement>    14

5.17 System.Linq.IOrderedEnumerable<T>    14

5.18 System.Linq.IOrderedQueryable    14

5.19 System.Linq.IOrderedQueryable<T>    15

5.20 System.Linq.IQueryable    15

5.21 System.Linq.IQueryable<T>    15

5.22 System.Linq.IQueryProvider    15

6 集合扩展方法    15

6.1 集合扩展方法的实现:一个Where的例子    15

6.2 延迟类    15

6.2.1 Select 选择    16

6.2.2 SelectMany 选择    16

6.2.3 Where 条件    16

6.2.4 OrderBy 排序升    17

6.2.5 OrderByDescending 排序降    17

6.2.6 GroupBy 分组    17

6.2.7 Join 联合查询    18

6.2.8 GroupJoin    18

6.2.9 Take 获取集合的前n个元素    19

6.2.10 Skip 跳过集合的前n个元素    19

6.2.11 Distinct 过滤集合中的相同项    19

6.2.12 Union 连接不同集合,自动过滤相同项    19

6.2.13 Concat 连接不同集合,不会自动过滤相同项    19

6.2.14 Intersect 获取不同集合的相同项(交集)    20

6.2.15 Except 从某集合中删除其与另一个集合中相同的项    20

6.2.16 Reverse 反转集合    20

6.2.17 TakeWhile 条件第一次不成立就跳出循环    20

6.2.18 SkipWhile 条件第一次不成立就失效,将后面的数据全取    20

6.2.19 Cast 将集合转换为强类型集合    21

6.2.20 OfType 过滤集合中的指定类型    21

6.3 不延迟(浅复本)    21

6.3.1 Single 集合中符合条件的唯一元素,浅复本    21

6.3.2 SingleOrDefault 集合中符合条件的唯一元素(没有则返回类型默认值),浅复本    21

6.3.3 First 集合的第一个元素,浅复本    21

6.3.4 FirstOrDefault 集合中的第一个元素(没有则返回类型默认值),浅复本    22

6.3.5 Last 集合中的最后一个元素,浅复本    22

6.3.6 LastOrDefault 集合中的最后一个元素(没有则返回类型默认值),浅复本    22

6.3.7 ElementAt 集合中指定索引的元素,浅复本    22

6.3.8 ElementAtOrDefault 集合中指定索引的元素(没有则返回类型默认值),浅复本    22

6.3.9 Contains 判断集合中是否包含有某一元素    22

6.3.10 Any 判断集合中是否有元素满足某一条件    22

6.3.11 All 判断集合中是否所有元素都满足某一条件    23

6.3.12 SequenceEqual 判断两个集合内容是否相同    23

6.3.13 Count 、LongCount集合中的元素个数    23

6.3.14 Average 、Sum集合平均值求和    23

6.3.15 Max、Min 集合最大值,最小值    24

6.3.16 Aggregate 根据输入的表达式获取一个聚合值    24

6.3.17 DefaultIfEmpty 查询结果为空则返回默认值,浅复本    24

6.3.18 ToArray 将集合转换为数组,浅复本    24

6.3.19 ToList 将集合转换为List<T>集合,浅复本    25

6.3.20 ToDictionary 将集合转换为<K, V>集合,浅复本    25

7 Lambda表达式    25

7.1 例1(比效)    25

7.2 例2(多参)    27

7.3 例3(list.Where)    27

7.4 Lambda表达式中Lifting    28

8 QuerySyntax 查询语法    29

8.1 from in select    30

8.2 orderby 排序    30

8.3 group by into 分组    31

8.4 join in on equals 联合查询    33

8.5 into 汇总    33

9 DataSource 数据绑定    34

所有数组都继承于System.Array,数组可以用System.Array建立

数组可以是一维也可以是多维数组。"维数"对应于用来识别每个数组元素的下标个数。维数可以多达 32 维。 数组的每一维都有一个非零的长度。在数组的每一维中,数组元素按下标 0 到该维最高下标值连续排列.数组没有固定大小。数组在创建以后仍可以改变大小。

数组是对象,因此每种数组类型都是一个单独的引用类型。这意味着:

1.数组变量中含有指向数据的指针,这些数据包含元素、秩和长度信息。

2.在将一个数组变量赋值给另一个数组变量时,只有指针进行了复制。

3.两个数组变量只有在具有相同的维数和元素数据类型时,才能看成是同一数据类型的数组变量。

基于System.Array定义数组

System.Array x=System.Array.CreateInstance( typeof ( string ),7);

x.SetValue("4",0);

x.SetValue("8",1);

x.SetValue("7",2);

x.SetValue("1",3);

x.SetValue("3",4);

x.SetValue("2",5);

x.SetValue("5",6);

System.Console.WriteLine(x.GetValue(5));

System.Collections.Generic. List <System.Windows.Forms. TextBox > jh = new List < TextBox >();

jh.Add( new TextBox ());

jh.Add( new TextBox ());

jh.Add( new TextBox ());

int i = 100;

foreach (System.Windows.Forms. TextBox tp in jh)

i = i + 100;

tp.Text =i.ToString();

tp.Left = i;

this .Controls.Add(tp);

System.Collections.Generic. List < a > jh = new List < a >();

jh.Add( new a ( "123" ));

jh.Add( new a ( "456" ));

jh.Add( new a ( "789" ));

foreach ( a tp in jh)

System. Console .WriteLine(tp.s);

System. Converter < TInput,TOutput >

public delegate TOutput Converter <TInput, TOutput>(TInput input);

将对象从一种类型转换为另一种类型的方法

public static void Main()

List < string > ls = new List < string >() { "1" , "2" , "3" };

//System.Converter<string, int> cw=new Converter<string,int>(stringToint);

//List<int> li = ls.ConvertAll<int>(cw);

List < int > li= ls.ConvertAll< int >(stringToint);

public static int stringToint( string s)

return int .Parse(s);

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" };

List < string > li = ls.ConvertAll< string >(stringAdd);

public static string stringAdd( string s)

return s+ "wxd" ;

static void Main( string [] args)

List < string > ls = new List < string >{ "wxd" , "lzm" };

// System.Action<string> w = new System.Action<string>(wxdPrint);

// ls.ForEach(w);

ls.ForEach(wxdPrint);

static void wxdPrint( string s)

Console .WriteLine(s);

Find、FindLast、FindIndex、FindLastIndex、Exists、TrueForAll

public T Find ( Predicate <T> match)

检索与条件匹配的所有元素,并返回整个 List 中的第一个匹配元素,其中包含与指定谓词所定义的条件相匹配的所有元素;否则为一个空 List

FindLast 返回索引值 public T FindLast ( Predicate <T> match)

FindIndex 返回索引值 public int FindIndex ( Predicate <T> match)

FindLastIndex 返回逻辑型 public int FindLastIndex ( Predicate <T> match)

Exists 返回逻辑型 public bool Exists ( Predicate <T> match)

TrueForAll 返回逻辑型,确定是否 List 中的每个元素都与指定的谓词所定义的条件相匹配 public bool TrueForAll ( Predicate <T> match)

List < int > ls = new List < int >() { 1, 2, 3 };

//System.Predicate<int> cw = new Predicate<int>(wxdFind);

// int a = ls.Find(cw);

int a = ls.Find(wxdFind);

public static bool wxdFind( int v)

bool b = (v >= 2); // 查找条件

return b;

FindAll

public List <T> FindAll ( Predicate <T> match)

检索与条件匹配的所有元素,如果找到,则为一个 List,其中包含与指定谓词所定义的条件相匹配的所有元素;否则为一个空 List

List < int > ls = new List < int >() { 1, 2, 3 };

// System.Predicate<int> cw = new Predicate<int>(WxdFind);

//List<int> li = ls.FindAll(cw);

List < int > li = ls.FindAll(WxdFind);

public static bool WxdFind( int v)

bool b = (v <= 2); // 查找条件

return b;

//以视图形式,不创建复本

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);

List < int > ls = new List < int >() { 1,2, 2, 3,4,5 };

//System.Predicate<int> cw = new Predicate<int>(WxdRem);

//int n = ls.RemoveAll(cw);

int n = ls.RemoveAll(WxdRem); //n为移除的个数

public static bool WxdRem( int v)

bool b = (v <= 2); // 条件

return b;

List < int > ls = new List < int >{1,3,2,4,5,0,8,1};

// System.Comparison<int> w = new Comparison<int>(wxdSort);

// ls.Sort(w);

ls.Sort(wxdSort);

static int wxdSort( int x, int y)

int v = x - y;

return v;

//return -v; //升序

//不改变原集合的顺序

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);

System.Collections

System.Collections.DictionaryEntry 结构 字典键/值对

散列表(也叫哈希表),是根据关键码值直接进行访问的数据结构,也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。

哈希表使用的是链式存储

哈希表使用键值对的方式来存储

哈希表中,键名不能重复

foreach ( object zd in x)

{ System.Collections.DictionaryEntry obj =(DictionaryEntry)zd;

System.Console.WriteLine(obj.Key);

System.Console.WriteLine(obj.Value);

System.Collections.Generic. SortedList < a , System.Windows.Forms. TextBox > jh= new SortedList < a , TextBox >();

a k1 = new a ( "123" ,0);

a k2 = new a ( "456" ,100);

a k3 = new a ( "789" ,200);

jh.Add(k1, new TextBox ());

jh.Add(k2, new TextBox ());

jh.Add(k3, new TextBox ());

//值操作

foreach ( TextBox tp in jh.Values)

this .Controls.Add(tp);

//键操作

foreach ( a tp in jh.Keys)

tp.s = "ok" ;

//键值对操作

foreach (System.Collections.Generic. KeyValuePair < a , TextBox > tp in jh)

tp.Value.Text = tp.Key.s;

tp.Value.Left = tp.Key.l;

//通过键操作值

jh[k2].Text = "hello" ;

System.Collections.Generic. LinkedList < a > lb = new LinkedList < a >();

System.Collections.Generic. LinkedListNode < a > p;

void 添加结点()

p = new LinkedListNode < a >( new a ( "1" ));

lb.AddFirst(p);

lb.AddAfter(lb.First, new a ( "2" ));

lb.AddBefore(lb.First, new a ( "3" ));

lb.AddLast( new a ( "4" ));

lb.AddFirst( new a ( "5" ));

void 读取头尾结点()

string fs = lb.First.Value.s;

string ls = lb.Last.Value.s;

System. Console .WriteLine(fs +ls);

void 下一节点( object sender, EventArgs e)

p = p.Next;

if (p == null )

System. Console .WriteLine( "到达边界" );

System. Console .WriteLine(p.Value.s);

void 上一结点()

p = p.Previous;

if (p == null )

System. Console .WriteLine( "到达边界" );

System. Console .WriteLine(p.Value.s);

static void Main( string [] args)

System.Linq.Expressions. Expression < lzm < int , bool >> ep;

ep = le => le > 123;

public class 集合 : System.Collections. CollectionBase , System.Collections. IEnumerable

//属性访问器

成员 this [ int index]

get { return ( 成员 )List[index] ; }

//添加成员

public int Add( 成员 v)

{ return List.Add(v);}

//实现GetEnumerator接口

public new System.Collections. IEnumerator GetEnumerator()

迭代器 d = new 迭代器 ( this );

return d;

//--------以下为迭代器类-------------

//迭代器子类

public class 迭代器 : System.Collections. IEnumerator

int index = -1; //索引用

集合 jh;

public 迭代器( 集合 v) //构造函数

{ jh = v; }

public void Reset() //重新初始化索引

{index = -1;}

public object Current //返回当前索引所指的对象

get { return jh.List[index];}

public bool MoveNext() //将索引指针向下移动一位

if (index < jh.List.Count-1)

index = index + 1;

return true ;

return false ;

//--------以下为成员类-------------

//迭代器中的数据成员

public class 成员

{ public string a;}

//第一种方法:显示使用

System.Collections. IEnumerator ee = j.GetEnumerator();

while (ee.MoveNext())

集合 . 成员 temp = ( 集合 . 成员 )ee.Current;

System. Console .WriteLine(temp.a );

//第二种方法:隐式使用

foreach ( 集合 . 成员 bb in j)

System. Console .WriteLine(bb.a);

public class wxd : System.Collections. IEnumerable

string [] v = { "a" , "b" , "c" , "d" , "e" , "f" , "g" };

public System.Collections. IEnumerator GetEnumerator()

for ( int i = 0; i < v.Length; i++)

yield return v[i];

yield return "wxd" ;

yield return "lzm" ;

//第一种方法:显示使用

System.Collections. IEnumerator ee = x.GetEnumerator();

while (ee.MoveNext())

string temp = ( string )ee.Current;

System. Console .WriteLine(temp);

//第二种方法:隐式使用

foreach ( string v in x)

System. Console .WriteLine(v);

ICollection 接口是 System.Collections 命名空间中类的基接口。

ICollection 接口扩展 IEnumerable;IDictionary 和 IList 则是扩展 ICollection 的更为专用的接口。IDictionary 实现是键/值对的集合,如 Hashtable 类。IList 实现是值的集合,其成员可通过索引访问,如 ArrayList 类。

某些集合(如 Queue 类和 Stack 类)限制对其元素的访问,它们直接实现 ICollection 接口。

如果 IDictionary 接口和 IList 接口都不能满足所需集合的要求,则从 ICollection 接口派生新集合类以提高灵活性。

ICollection <T>

System.Collections.Generic

定义操作泛型集合的方法。

IList

System.Collections

IList 是 ICollection 接口的子代,并且是所有非泛型列表的基接口。IList 实现有三种类别:只读、固定大小和可变大小。无法修改只读 IList。固定大小的 IList 不允许添加或移除元素,但允许修改现有元素。可变大小的 IList 允许添加、移除和修改元素。

IList <T>

System.Collections.Generic

表示可按照索引单独访问的一组对象。

IEqualityComparer

System.Collections

对象的相等比较

IEqualityComparer <T>

System.Collections.Generic

定义方法以支持对象的相等比较。

IDictionary

System.Collections

键/值对基接口

IDictionary <TKey,TValue>

System.Collections.Generic

表示键/值对的泛型集合。

IDictionaryEnumerator

System.Collections

枚举字典的元素

IComparer

System.Collections

比较两个对象的方法

接口方法说明 int Compare(object x, object y)

static void Main( string [] args)

ArrayList ls = new ArrayList () { new wxd { value = 4 }, new wxd { value = 2 }, new wxd { value = 1 }, new wxd { value = 3 } };

ls.Sort( new wxd ());

class wxd : System.Collections. IComparer

public int value;

public int Compare( object x, object y)

int v = (( wxd )x).value - (( wxd )y).value;

return v;

static void Main( string [] args)

List < wxd > ls = new List < wxd > { new wxd { value = 4 }, new wxd { value = 2 }, new wxd { value = 1 }, new wxd { value = 3 } };

ls.Sort( new wxd ());

class wxd : System.Collections.Generic. Comparer < wxd >

public int value;

public override int Compare( wxd x, wxd y)

int v = x.value - y.value;

return v;

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;

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 >() { "wxd/1" , "lzm/2" , "wxwinter/3" };

var li = ls.SelectMany(p => p.Split( '/' ));

foreach ( var s in li)

Console .WriteLine(s);

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

//以视图形式,不创建复本

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);

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, 3, 2, 4, 5, 0, 8, 1 };

var li = ls.OrderBy(i => i);

//----

ls[1] = 6666;

ls.Add(-5);

//---li中的顺序已重新排列

//不改变原集合的顺序

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; })

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);

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;

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;

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;

//返回一个新的匿名类的集合

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() };

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]

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);

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);

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);

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);

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 >();

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);

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的

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的

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;

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;

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;

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

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

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;

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;

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 累乘

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;

List < string > ls3 = new List < string >() { "wxd" , "lzm" , "wxwinter" , "wxd" };

var ls4 = ls3.Where(p => p == "ok" ).DefaultIfEmpty( "not" ); ;

//如果不存在返回给集合一个自定义的对象

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 集合

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;

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;

//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的值即可。因为编译器会对该临时变量重新分配内存,这样,每次循环,都重新分配新的内存,就不会有这个问题

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;

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;

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() ;

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;

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;

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);

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;

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;

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" ;

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();