static void Main(string[] args)
{
#region Aggregate 把集合中的元素按照表达式依次执行
{
IEnumerable<int> list = new List<int>
{
1,2,3,11
};
int z1 = list.Aggregate((x, y) => x + y); // (((1+2)+3)+4)
Console.WriteLine(z1.ToString());
int z2 = list.Aggregate(3, (x, y) => x + y); //((((3+1)+2)+3)+4)
Console.WriteLine(z2.ToString());
int z3 = list.Aggregate(0, (x, y) => x + y, x => x * 2); // (((1+2)+3)+4) * 2
Console.WriteLine(z3.ToString());
Console.WriteLine("*****************************Aggregate*******************************");
}
#endregion
#region All 判断集合中的每个元素是否都匹配某个表达式
{
IEnumerable<int> list = new List<int>
{
1,2,3,11
};
bool pd1 = list.All(x => x > 10);
bool pd2 = list.All(x => x < 10);
Console.WriteLine(pd1.ToString());
Console.WriteLine(pd2.ToString());
Console.WriteLine("*****************************All*******************************");
}
#endregion
#region Any 判断集合是否为空或者判断结合中某个元素满足表达式的条件
{
List<int> list = new List<int>();
bool angPd = list.Any(); //集合中没有元素false
Console.WriteLine(angPd);
list.Add(1);
angPd = list.Any(); //集合中有元素true
Console.WriteLine(angPd);
IEnumerable<int> list1 = new List<int>
{
1,2,3,11
};
angPd = list1.Any(x => x > 10);//集合中只要有一个元素满足表达式条件就为true 否则为false
Console.WriteLine(angPd);
angPd = list1.Any(x => x > 12);
Console.WriteLine(angPd);
angPd = list1.Any(x => x < 11);
Console.WriteLine(angPd);
Console.WriteLine("*****************************Any*******************************");
}
#endregion
#region AsEnumerable 转换一个远程IQueryable类型为本地IEnumerable 类型
{
DataTable table = new DataTable();
EnumerableRowCollection rows = table.AsEnumerable();
Console.WriteLine("*****************************AsEnumerable*******************************");
}
#endregion
#region Average 如果类型为Nullable<T>类型则在算集合平均数的时候 抛弃掉所有null的值然后计算平均值 能够有的类型为int,long,double,float,decimal和他们的Nullable版本
//int
{
List<int> intList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double intAven = intList.Average();
Console.WriteLine(intAven);
List<int?> intNulList = new List<int?> { 1, 2, 3, null, null, null, 7, 8, 9, 10 };
double? intNullAven = intNulList.Average();
Console.WriteLine(intNullAven);
Console.WriteLine("*****************************Average*******************************");
}
#endregion
#region Cast 转换一个继承IEnumerable的非泛型版本 为IEnumerable<T> 如果无法转换则抛出异常
{
string[] sList = new string[3] { "1", "2", "3" };
IEnumerable<string> intCaseList = sList.Cast<string>();
foreach (var item in intCaseList)
{
Console.WriteLine(item.ToString());
}
Console.WriteLine("*****************************Cast*******************************");
}
#endregion
#region Concat 将List<T1> 和 List<T2> 合并为 List<T> 不去除重复项
{
List<int> concatList1 = new List<int> { 1, 2, 3, 4, 5};
List<int> concatList2 = new List<int> { 5, 6, 7, 8 };
IEnumerable<int> concatList3 = concatList1.Concat(concatList2);
foreach (var item in concatList3)
{
Console.WriteLine(item.ToString());
}
Console.WriteLine("*****************************Concat*******************************");
}
#endregion
#region Contains 比较一个T 是否存在于List<T> 可以继承IEqualityComparer<T>接口来指定判断依据
{
IEnumerable<int> a = new List<int> { 1, 2, 3, 4, 5, 6 };
bool containsPd = a.Contains(1);
Console.WriteLine(containsPd);
List<People> peoples = new List<People> {
new People
{
Name = "王"
}};
People people2 = new People
{
Name = "王"
};
containsPd = peoples.Contains(people2, new People());
Console.WriteLine(containsPd);
Console.WriteLine("*****************************Contains*******************************");
}
#endregion
#region Count 获得集合中的元素数量,或者得到满足一个表达式的所有元素的数量
{
IEnumerable<int> countList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int count1 = countList.Count();
int count2 = countList.Count(x => x > 5);
Console.WriteLine(count1);
Console.WriteLine(count2);
Console.WriteLine("*****************************Count*******************************");
}
#endregion
#region DefaultIfEmpty 如果集合中有元素则返回集合值 如果为空则返回集合类型的默认值,并且如果DefaultIfEmpty设置了空集合的默认值,则会返回一个包含了默认值的集合
{
IEnumerable<int> defaultIfEmptyList = new List<int> { };
foreach (var item in defaultIfEmptyList.DefaultIfEmpty())
{
Console.WriteLine(item);
};//值类型返回0
IEnumerable<People> peList = new List<People> { };
foreach (var item in peList.DefaultIfEmpty())
{
if (item == null)
Console.WriteLine("null");
else
Console.WriteLine(item.GetType());
};//引用类型返回null
IEnumerable<bool> bList1 = new List<bool> { };
foreach (var item in bList1.DefaultIfEmpty())
{
Console.WriteLine(item);
};//布尔类型返回false
List<int> list3 = new List<int> { };
list3 = defaultIfEmptyList.DefaultIfEmpty(5).ToList();
Console.WriteLine(list3[0]);
Console.WriteLine("*****************************DefaultIfEmpty*******************************");
}
#endregion
#region Distinct Distinct将去除重复的集合中的数据并返回一个新集合,如果集合类型是引用类型需要一个继承IEqualityComparer<T>的接口的判断类作为判断条件
{
List<int> list = new List<int> { 1, 2, 3, 4, 5, 5, 6, 6, 7, 8, 8 };
foreach(var item in list.Distinct())
{
Console.WriteLine(item);
}
List<People> peoples = new List<People>
{
new People(){ Name = "王"},
new People(){ Name = "王"}
};
foreach (var item in peoples.Distinct())
{
Console.WriteLine(item.Name);
}
Console.WriteLine("************************************************************");
foreach (var item in peoples.Distinct(new People()))
{
Console.WriteLine(item.Name);
}
Console.WriteLine("*****************************Distinct*******************************");
}
#endregion
#region ElementAt 获得集合中的第n个元素 从0开始 如果超过集合长度则报错
{
List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
var item = list.ElementAt(4);
Console.WriteLine(item);
Console.WriteLine("*****************************ElementAt*******************************");
}
#endregion
#region ElementAtOrDefault 获得集合中的第n个元素从0开始 如果超过集合长度则给集合类型默认值
{
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
var item1 = list.ElementAtOrDefault(1);
var item2 = list.ElementAtOrDefault(10);
Console.WriteLine(item1);
Console.WriteLine(item2);
List<People> peoples = new List<People>
{
new People(){ Name = "aa"}
};
var item3 = peoples.ElementAtOrDefault(0);
var item4 = peoples.ElementAtOrDefault(1);
Console.WriteLine(item3);
if (item4 == null)
Console.WriteLine("null");
Console.WriteLine("*****************************ElementAt*******************************");
}
#endregion
#region Empty 获得一个没有分配内存的空的集合
{
IEnumerable<int> list = Enumerable.Empty<int>();
Console.WriteLine("*****************************Empty*******************************");
}
#endregion
#region Except 获得list2中没有的list1元素 可以使用继承了IEqualityComparer的类重新定义相等判断
{
IEnumerable<int> list1 = new List<int> { 1, 2, 3, 4, 5, 6 };
IEnumerable<int> list2 = new List<int> { 4, 5, 6, 7, 8, 9 };
IEnumerable<int> list3 = list1.Except(list2);
foreach(var item in list3)
{
Console.WriteLine(item);
}
IEnumerable<People> peoples1 = new List<People>
{
new People{ Name = "王"}
};
IEnumerable<People> peoples2 = new List<People>
{
new People{ Name = "王"}
};
IEnumerable<People> peoples3 = peoples1.Except(peoples2);
foreach(People item in peoples3)
{
Console.WriteLine(item);
}
IEnumerable<People> peoples4 = peoples1.Except(peoples2,new People());
Console.WriteLine(peoples4.Count());
Console.WriteLine("*****************************Except*******************************");
}
#endregion
#region First 获得集合中第一个元素 或者返回满足表达式的第一个元素(可以多个元素符合) 如果没有元素则报错
{
IEnumerable<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
int a = list1.First();
Console.WriteLine(a.ToString());
int b = list1.First(x => x > 4);
Console.WriteLine(b.ToString());
Console.WriteLine("*****************************First*******************************");
}
#endregion
#region FirstOrDefault 获得集合中第一个元素 或者返回满足表达式的第一个元素 如果集合中没有元素则 返回集合类型的默认值
{
List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
int a = list1.FirstOrDefault();
Console.WriteLine(a);
int b = list1.FirstOrDefault(x => x > 4);
Console.WriteLine(b);
int c = list1.FirstOrDefault(x => x > 5);
Console.WriteLine(c);
List<People> peoples = new List<People>();
People people = peoples.FirstOrDefault();
if (people == null)
Console.WriteLine("null");
else
Console.WriteLine(people.Name);
Console.WriteLine("*****************************FirstOrDefault*******************************");
}
#endregion
#region GroupBy 按照给定的key分组
{
IEnumerable<GroupClass> groups1 = new List<GroupClass>
{
new GroupClass(){ Number = 1,Name = "王",Address = "上海"},
new GroupClass(){ Number = 2,Name = "王",Address = "北京"},
new GroupClass(){ Number = 3,Name = "吴",Address = "上海"},
new GroupClass(){ Number = 4,Name = "成",Address = "杭州"},
new GroupClass(){ Number = 5,Name = "成",Address = "杭州"},
new GroupClass(){ Number = 6,Name = "吴",Address = "上海"}

};
var peoples1 = groups1.GroupBy(x => x.Name).Select(item=> new { Name = item.Key,num = item.Sum(x=>x.Number)});//public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
foreach(var people in peoples1)
{
Console.WriteLine(people.Name);
Console.WriteLine(people.num);
}
var peoples2 = groups1.GroupBy(x => x.Name, (k, g) => new { name = k, value = g }).Select(item =>new { Name = item.name,num = item.value.Sum(x=>x.Number)} );
foreach(var item in peoples2)
{
Console.WriteLine(item.Name);
Console.WriteLine(item.num);
};
var peoples3 = groups1.GroupBy(x => x.Name, (x) => new {Name = x.Name + "AAA",num = x.Number * 5}, (k, g) => new { name = k, value = g }).Select(item=>new { Name =item.name,value=item.value.Sum(x=>x.num)});
foreach (var item in peoples3)
{
Console.WriteLine(item.Name);
Console.WriteLine(item.value);
};
var peoples4 = groups1.GroupBy(x => x.Name, (x) => new { Name = x.Name + "AAA", num = x.Number * 5, Ve = "3" });
IEnumerable<People> peoples = new List<People>
{
new People{ Name = "a",num = 3,Address = "a1"},
new People{ Name = "a",num = 3,Address = "a2"},
new People{ Name = "b",num = 5,Address = "a3"},
new People{ Name = "b",num = 5,Address = "a4"},
new People{ Name = "b",num = 7,Address = "a5"},
new People{ Name = "c",num = 7,Address = "a6"},
new People{ Name = "d",num = 9,Address = "a7"},
};
var peoples5 = peoples.GroupBy(x => new { x.Name, x.num },x=>x.Address);
foreach (var item in peoples5)
{
Console.WriteLine(item.Key.Name);
Console.WriteLine(item.Key.num);
foreach (var value in item)
{
Console.WriteLine(value);
}
}
Console.WriteLine("*****************************GroupBy*******************************");
}
#endregion
#region GroupJoin 获得一个左连接类似字典的集合
{
List<GClass1> c1 = new List<GClass1>
{
new GClass1{ G1Id = 1,Name = "1"},
new GClass1{ G1Id = 2,Name = "2"},
new GClass1{ G1Id = 3,Name = "3"},
new GClass1{ G1Id = 4,Name = "4"},
new GClass1{ G1Id = 5,Name = "5"},
new GClass1{ G1Id = 6,Name = "6"},
new GClass1{ G1Id = 7,Name = "7"}
};
List<GClass2> c2 = new List<GClass2>
{
new GClass2{ Id = 1,Value = "1a"},
new GClass2{ Id = 1,Value = "1b"},
new GClass2{ Id = 2,Value = "2a"},
new GClass2{ Id = 2,Value = "2b"},
new GClass2{ Id = 2,Value = "2c"},
new GClass2{ Id = 3,Value = "3a"},
new GClass2{ Id = 3,Value = "3b"},
new GClass2{ Id = 4,Value = "4a"},
new GClass2{ Id = 9,Value = "9a"}
};
var list = c1.GroupJoin(c2, x => x.G1Id, y => y.Id, (x, y) => new { x, y });
Console.WriteLine("*****************************GroupJoin*******************************");
}
#endregion
#region Intersect 返回 list1和list2中相同的数据 或者根据表达式规则返回
{
IEnumerable<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
IEnumerable<int> list2 = new List<int> { 2, 3, 4, 5, 6 };
IEnumerable<int> list3 = list1.Intersect(list2);
foreach (var item in list3)
{
Console.WriteLine(item);
}
IEnumerable<People> peoples1 = new List<People>
{
new People { Name = "王"}
};
IEnumerable<People> peoples2 = new List<People>
{
new People { Name = "王"}
};
IEnumerable<People> list4 = peoples1.Intersect(peoples2);
IEnumerable<People> list5 = peoples1.Intersect(peoples2,new People());
Console.WriteLine(list4.Count());
Console.WriteLine(list5.Count());
Console.WriteLine("*****************************Intersect*******************************");
}
#endregion
#region Join 连接2个集合
{
List<GClass1> c1 = new List<GClass1>
{
new GClass1{ G1Id = 1,Name = "1"},
new GClass1{ G1Id = 2,Name = "2"},
new GClass1{ G1Id = 3,Name = "3"},
new GClass1{ G1Id = 4,Name = "4"},
new GClass1{ G1Id = 5,Name = "5"},
new GClass1{ G1Id = 6,Name = "6"},
new GClass1{ G1Id = 7,Name = "7"}
};
List<GClass2> c2 = new List<GClass2>
{
new GClass2{ Id = 1,Value = "1a"},
new GClass2{ Id = 1,Value = "1b"},
new GClass2{ Id = 2,Value = "2a"},
new GClass2{ Id = 2,Value = "2b"},
new GClass2{ Id = 2,Value = "2c"},
new GClass2{ Id = 3,Value = "3a"},
new GClass2{ Id = 3,Value = "3b"},
new GClass2{ Id = 4,Value = "4a"},
new GClass2{ Id = 9,Value = "9a"}
};

var qure = from a in c1
join b in c2 on a.G1Id equals b.Id
into c3
from c in c3.DefaultIfEmpty()
select
new
{
Id = a.G1Id,
Value = c
};

var list = c1.Join(c2, x => x.G1Id, y => y.Id, (x, y) => new { x, y }).DefaultIfEmpty();
foreach (var item in list)
{
// Console.WriteLine(item.Id);
}
}
Console.WriteLine("*****************************Join*******************************");
#endregion
#region Last 返回数组中的最后项,或者满足表达式的最后项 如果没有则报错
{
IEnumerable<int> list = new List<int> { 1, 2, 3, 4, 5 };
int a = list.Last();
int b = list.Last(x => x < 3);
Console.WriteLine(a);
Console.WriteLine(b);
}
Console.WriteLine("*****************************Last*******************************");
#endregion
#region LastOrDefault 返回数组中的最后项,或者满足表达式的最后项 如果没有则返回集合类型的默认值
{
IEnumerable<int> list = new List<int> { 1, 2, 3, 4, 5 };
int a = list.LastOrDefault();
int b = list.LastOrDefault(x => x > 6);
Console.WriteLine(a);
Console.WriteLine(b);
}
Console.WriteLine("*****************************LastOrDefault*******************************");
#endregion
#region LongCount 返回一个int64的集合的元素数量,或者一个int64的满足表达式的集合元素数量
{
IEnumerable<int> list = new List<int> { 1, 2, 3, 4, 5 };
long a = list.LongCount();
long b = list.LongCount(x => x > 3);
Console.WriteLine(a);
Console.WriteLine(b);
}
Console.WriteLine("*****************************LongCount*******************************");
#endregion
#region max 获得集合中的最大值 或者满足某个表达式的最大值,可以有的类型为int long double float decimal和对应的Nullable,类需要继承IComparable 来比较最大值
{ // public static TSource Max<TSource>(this IEnumerable<TSource> source);
IEnumerable<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
int a = list1.Max();
Console.WriteLine(a);
IEnumerable<int?> list2 = new List<int?> { null};
int? b = list2.Max();
Console.WriteLine(b);
IEnumerable<People> peoples = new List<People> {
new People{ num = 1},
new People{ num = 2},
new People{ num = 3},
new People{ num = 4},
};
People people = peoples.Max();
Console.WriteLine(people.num);
int c = list1.Max(x => x * 3);
Console.WriteLine(c);
Console.WriteLine("*****************************max*******************************");
}
#endregion
#region Min 获得集合中的最小值 或者满足某个表达式的最小值,可以有的类型为int long double float decimal和对应的Nullable,类需要继承IComparable 来比较最小值
{
IEnumerable<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
int a = list1.Min();
Console.WriteLine(a);
IEnumerable<int?> list2 = new List<int?> { null };
int? b = list2.Min();
Console.WriteLine(b);
IEnumerable<People> peoples = new List<People> {
new People{ num = 1},
new People{ num = 2},
new People{ num = 3},
new People{ num = 4},
};
People people = peoples.Min();
Console.WriteLine(people.num);
int c = list1.Min(x => x * 3);
Console.WriteLine(c);
Console.WriteLine("*****************************Min*******************************");
}
#endregion
#region OfType 把IEnumerable 类型的集合按照给定的类型重新组合成IEnumerable<T> 类型 如果没有给定的类型 则返回一个该类型空的集合
{
ArrayList array = new ArrayList { 1, "1", 2, "abc", null, 3, new People() };
IEnumerable<int> list1 = array.OfType<int>();
foreach(int item in list1)
{
Console.WriteLine(item);
}
IEnumerable<string> list2 = array.OfType<string>();
foreach (string item in list2)
{
Console.WriteLine(item);
}
Console.WriteLine("*****************************OfType*******************************");
}
#endregion
#region OrderBy 更具给定的条件 从小到大排序
{
IEnumerable<int> list1 = new List<int> { 2, 1, 3, 6, 5 };
IOrderedEnumerable<int> list2 = list1.OrderBy(x=>x);
foreach(var item in list2)
{
Console.WriteLine(item);
}
IEnumerable<People> peoples = new List<People>
{
new People{ num = 1,Name="a1"},
new People{ num = 3,Name="a3"},
new People{ num = 4,Name="a4"},
new People{ num = 2,Name="a2"}
};
IEnumerable<People> peoples2 = peoples.OrderBy(x => x.num );
foreach(var item in peoples2)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("*****************************OfType*******************************");
}
#endregion
#region OrderByDescending 更具给定的条件 从大到小排序
{
IEnumerable<int> list1 = new List<int> { 2, 1, 3, 6, 5 };
IOrderedEnumerable<int> list2 = list1.OrderByDescending(x => x);
foreach (var item in list2)
{
Console.WriteLine(item);
}
IEnumerable<People> peoples = new List<People>
{
new People{ num = 1,Name="a1"},
new People{ num = 3,Name="a3"},
new People{ num = 4,Name="a4"},
new People{ num = 2,Name="a2"}
};
IEnumerable<People> peoples2 = peoples.OrderByDescending(x => x.num);
foreach (var item in peoples2)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("*****************************OrderByDescending*******************************");
}
#endregion
#region Range 生成一个int的集合 从a开始之后的b个数
{
IEnumerable<int> list = Enumerable.Range(7, 15);
foreach(var item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("*****************************Range*******************************");
}
#endregion
#region Repeat 按照类型生成重复b次生成a
{
IEnumerable<int> list1 = Enumerable.Repeat(1, 5);
foreach(var item in list1)
{
Console.WriteLine(item);
}
Console.WriteLine("*****************************Range*******************************");
}
#endregion
#region Reverse 把集合中的元素倒置
{
IEnumerable<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
IEnumerable<int> list2 = list1.Reverse();
foreach (var item in list2)
{
Console.WriteLine(item);
}
Console.WriteLine("*****************************Reverse*******************************");
}
#endregion
#region Select 按照表达式筛选集合并返回一个集合
{
IEnumerable<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
IEnumerable<int> list2 = list1.Select(x => x * 3);
List<People> peoples = new List<People>
{
new People{ Name="1a" ,num = 1},
new People{ Name="2a" ,num = 2},
new People{ Name="3a" ,num = 3},
new People{ Name="4a" ,num = 4},
new People{ Name="5a" ,num = 5},
};
var list3 = peoples.Select(x => new
{
Name = x.Name,
Value = x.num * 10
});
foreach(var item in list3)
{
Console.WriteLine(item);
}
Console.WriteLine("*****************************Select*******************************");
}
#endregion
#region SelectMany 获得集合中的元素的子集合 并把这些子类型合并为一个集合
{
IEnumerable<Department> departments = new List<Department>
{
new Department{ Name = "开发1",
List = new List<Personnel>
{
new Personnel{ Name = "1",Age = 10,Sex="男"},
new Personnel{ Name = "2",Age = 20,Sex="男"},
new Personnel{ Name = "3",Age = 20,Sex="女"},
new Personnel{ Name = "4",Age = 30,Sex="女"},
}
}, new Department{ Name = "开发2",
List = new List<Personnel>
{
new Personnel{ Name = "21",Age = 15,Sex="男"},
new Personnel{ Name = "22",Age = 20,Sex="男"},
new Personnel{ Name = "23",Age = 15,Sex="女"},
new Personnel{ Name = "24",Age = 25,Sex="女"},
}
}, new Department{ Name = "开发3",
List = new List<Personnel>
{
new Personnel{ Name = "31",Age = 15,Sex="男"},
new Personnel{ Name = "32",Age = 49,Sex="男"},
new Personnel{ Name = "33",Age = 20,Sex="女"},
new Personnel{ Name = "34",Age = 25,Sex="女"},
}
}
};
IEnumerable<Personnel> list1 = departments.SelectMany(x => x.List);
foreach(var item in list1)
{
Console.WriteLine(item.Name);
}
var list2 = departments.SelectMany(x => x.List,(x,y) => new { x.Name,y.Age});
foreach (var item in list2)
{
Console.WriteLine(item.Name + "/" + item.Age);
}
Console.WriteLine("*****************************SelectMany*******************************");
}
#endregion
#region SequenceEqual 比较2个集合中的所有元素是否相等(按照顺序)或者按照继承IEqualityComparer的比较
{
IEnumerable<int> list1 = new List<int> { 1, 2, 4, 3, 5 };
IEnumerable<int> list2 = new List<int> { 1, 2, 3, 4, 5 };
bool pd = list1.SequenceEqual(list2);
Console.WriteLine(pd);
IEnumerable<string> list3 = new List<string> { "123", "234", "456" };
IEnumerable<string> list4 = new List<string> { "123", "234", "456" };
bool pd1 = list3.SequenceEqual(list4);
Console.WriteLine(pd1);
IEnumerable<People> peoples1 = new List<People>
{
new People { Name = "王"},
new People { Name = "吴"}
};
IEnumerable<People> peoples2 = new List<People>
{
new People { Name = "王"},
new People { Name = "吴"}
};
bool pd2 = peoples1.SequenceEqual(peoples2,new People());
Console.WriteLine(pd2);
Console.WriteLine("*****************************SequenceEqual*******************************");
}
#endregion
#region Single 返回满足表达式的项 但必须是唯一 如果没有找到或者找到多个将报错
{
IEnumerable<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
//int a= list1.Single(x => x > 3); 出错
int a = list1.Single(x => x > 4);
Console.WriteLine(a);
Console.WriteLine("*****************************Single*******************************");
}
#endregion
#region SingleOrDefault 返回满足表达式的项 但必须是唯一 如果没有找到则按照集合类型给与默认值,到符合条件的超过1个还是报错
{
IEnumerable<int> list = new List<int> { 1, 2, 3, 4, 5 };
int a = list.SingleOrDefault(x => x > 5);
Console.WriteLine(a);
Console.WriteLine("*****************************SingleOrDefault*******************************");
}
#endregion
#region Skip 跳过集合的n个元素返回剩余的元素
{
IEnumerable<int> list = new List<int> { 1, 2, 3, 4, 5 };
IEnumerable<int> list2 = list.Skip(2);
foreach(var item in list2)
{
Console.WriteLine(item);
}
Console.WriteLine("*****************************Skip*******************************");
}
#endregion
#region SkipWhile 从第一个元素开始匹配 跳过满足表达式的n个元素并返回剩下元素 当判断条件第一次为false的时候 返回false后的全部元素
{
IEnumerable<int> list = new List<int> { 3, 4,5,6,1,2,3,4,5 };
IEnumerable<int> list2 = list.SkipWhile(x => x > 2);
foreach (var item in list2)
{
Console.WriteLine(item);
}
Console.WriteLine("*****************************SkipWhile*******************************");
}
#endregion
#region Sum 对集合中的元素求和符合的类型为 int long double float decimal 和对应的 Nullable类型
{
IEnumerable<int> list = new List<int> { 1, 2, 3, 4, 5 };
int a = list.Sum();
int b = list.Sum(x => x * 3);
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine("*****************************Sum*******************************");
}
#endregion
#region Take 返回集合的前n个元素 超过元素集合数量不抱错 并返回全部集合
{
IEnumerable<int> list = new List<int> { 1, 2, 3, 4, 5 };
IEnumerable<int> list1 = list.Take(7);
foreach(var item in list1)
{
Console.WriteLine(item);
}
Console.WriteLine("*****************************Take*******************************");
}
#endregion
#region TakeWhile 从第一个元素开始匹配 返回满足表达式的元素 当判断条件第一次为false时 抛弃剩余元素
{
IEnumerable<int> list = new List<int> { 3, 4, 5, 1, 2, 3, 4, 5, 6 };
IEnumerable<int> list1 = list.TakeWhile(x => x > 2);
foreach(var item in list1)
{
Console.WriteLine(item);
}
Console.WriteLine("*****************************TakeWhile*******************************");
}
#endregion
#region ThenBy OrderBy后的排序按照从小到大排序 或者按照继承IComparer接口的排序规则排序
{
IEnumerable<People> peoples = new List<People>
{
new People{ num = 1,Name = "1"},
new People{ num = 2,Name = "2"},
new People{ num = 3,Name = "6"},
new People{ num = 3,Name = "5"},
new People{ num = 3,Name = "4"},
new People{ num = 4,Name = "3"},
};
IOrderedEnumerable<People> peoples1 = peoples.OrderBy(x => x.num).ThenBy(x => x.Name);
foreach(var item in peoples1)
{
Console.WriteLine(item.num + "/" + item.Name);
}
Console.WriteLine("*****************************ThenBy*******************************");
}
#endregion
#region ThenByDescending OrderBy后的排序按照从大到小排序 或者按照继承IComparer接口的排序规则排序
{
IEnumerable<People> peoples = new List<People>
{
new People{ num = 1,Name = "1"},
new People{ num = 2,Name = "2"},
new People{ num = 3,Name = "6"},
new People{ num = 3,Name = "5"},
new People{ num = 3,Name = "4"},
new People{ num = 4,Name = "3"},
};
IOrderedEnumerable<People> peoples1 = peoples.OrderBy(x => x.num).ThenByDescending(x => x.Name);
foreach (var item in peoples1)
{
Console.WriteLine(item.num + "/" + item.Name);
}
Console.WriteLine("*****************************ThenByDescending*******************************");
}
#endregion
#region ToArray 转换IEnumerable<T> 为对应类型数组
{
IEnumerable<int> list = new List<int> { 1, 2, 3, 4, 5 };
int[] arrayList = list.ToArray();
Console.WriteLine("*****************************ToArray*******************************");
}
#endregion
#region ToDictionary 把IEnumerable<T> 类型装成 Dictionary<Key,Value>类型
{
IEnumerable<People> peoples = new List<People>
{
new People{ Name="1",num=1},
new People{ Name="2",num=21},
new People{ Name="3",num=22},
new People{ Name="4",num=3},
new People{ Name="5",num=41},
new People{ Name="6",num=42},
};
Dictionary<string,People> de1 = peoples.ToDictionary(x => x.Name);
Dictionary<string, int> de2 = peoples.ToDictionary(x => x.Name, y => y.num);
Console.WriteLine("*****************************ToDictionary*******************************");
}
#endregion
#region ToList 把IEnumerable<T> 类型转为 List<T>类型
{
IEnumerable<int> list = new List<int>{1,2,3,4,5 };
List<int> list1 = list.ToList();
Console.WriteLine("*****************************ToList*******************************");
}
#endregion
#region ToLookup 返回一个key value类型的集合 并且集合不能被修改
{
IEnumerable<People> peoples = new List<People>
{
new People{ Name = "1",num = 11},
new People{ Name = "1",num = 12},
new People{ Name = "1",num = 13},
new People{ Name = "2",num = 21},
new People{ Name = "2",num = 22},
new People{ Name = "2",num = 23},
new People{ Name = "2",num = 24},
new People{ Name = "3",num = 31},
new People{ Name = "3",num = 32},
new People{ Name = "4",num = 41},
};
ILookup<string,People> list = peoples.ToLookup(x => x.Name);
foreach (var item in list)
{
Console.WriteLine(item.Key);
foreach (var value in item)
{
Console.WriteLine(value.num);
}
}
Console.WriteLine("*****************************ToLookup*******************************");
}
#endregion
#region Union 合并2个集合并删除重复的部分
{
IEnumerable<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
IEnumerable<int> list2 = new List<int> { 2, 4, 7, 8, 9 };
IEnumerable<int> list3 = list1.Union(list2);
foreach(var item in list3)
{
Console.WriteLine(item);
}
IEnumerable<People> peoples1 = new List<People>
{
new People{ Name="王"},
new People { Name = "吴"}
};
IEnumerable<People> peoples2 = new List<People>
{
new People{ Name="王"},
};
IEnumerable<People> peoples3 = peoples1.Union(peoples2);
IEnumerable<People> peoples4 = peoples1.Union(peoples2,new People());
Console.WriteLine("*****************************Union*******************************");
}
#endregion
#region Where 更具条件返回筛选后的集合
{
IEnumerable<int> list = new List<int> { 1, 2, 3 };
list.Where((x,y) => x < 4);
Console.WriteLine("*****************************Where*******************************");
}
#endregion
#region Zip 把2个集合中的元素1对1 执行表达式后返回一个新集合 集合中的个数为2个集合中的较少元素的集合个数一样
{
IEnumerable<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
IEnumerable<int> list2 = new List<int> { 4, 5, 6, 7 };
IEnumerable<int> list3 = list1.Zip(list2,(x,y) => x + y);
foreach(var item in list3)
{
Console.WriteLine(item);
}
}
#endregion
Console.ReadLine();
}

Linq To Object 函数介绍的更多相关文章

  1. Linq To SQL和Linq To Object的批量操作InsertAllOnSubmit介绍

    无论是Linq To SQL还是Linq To Object(Entity frameworks)它们都为开发人员提供了Insert操作,及Insert集合操作,即InsertOnSubmit和Ins ...

  2. (第一篇) 一步一步带你了解linq to Object

    要想学好linq to object 我们必须要先学习lambda 表达式,学习lambda 表达式呢我们必须了解匿名函数和匿名类及扩展方法,学习匿名函数,我们必须学会委托,这是本文的宗旨.下面开始第 ...

  3. LINQ&EF任我行(二)--LinQ to Object

    (原创:灰灰虫的家http://hi.baidu.com/grayworm)LinQ to Objects是LinQ家庭的核心,其它的LinQ也使用了与LinQ to Objects相同的查询句法.最 ...

  4. 在net中json序列化与反序列化 面向对象六大原则 (第一篇) 一步一步带你了解linq to Object 10分钟浅谈泛型协变与逆变

    在net中json序列化与反序列化   准备好饮料,我们一起来玩玩JSON,什么是Json:一种数据表示形式,JSON:JavaScript Object Notation对象表示法 Json语法规则 ...

  5. .NET面试题系列[13] - LINQ to Object

    .NET面试题系列目录 名言警句 "C# 3.0所有特性的提出都是更好地为LINQ服务的" - Learning Hard LINQ是Language Integrated Que ...

  6. LinQ To Object 基本用法

    http://www.cnblogs.com/terryzh/archive/2012/11/10/2763538.html LinQ To Object 基本用法 inq的基本语法:var resu ...

  7. 1.解剖Linq to object

    LINQ想必大家都不陌生了,它的出现使得我们的代码变得更短.更优雅了.至于LINQ是什么,Linq to object这类的扩展方法到底做了些什么.我们使用的EF是如何实现的(如何解析Expressi ...

  8. Linq to EF 与Linq to Object 使用心得

    大家都知道Linq既可以用来查询数据库对象(我这里指的是Entity FrameWork里的Model对象),也可以用来查询内存中的IEnumerable对象. 两者单独查询时都不会出现什么问题,不过 ...

  9. Linq to OBJECT延时标准查询操作符

    1.Where 操作符用于限定输入集合中的元素,将符合条件的元素组织声称一个序列结果.2.Select  操作符用于根据输入序列中的元素创建相应的输出序列中的元素,输出序列中的元素类型可以与输入序列中 ...

随机推荐

  1. ViewPager+Fragment实现滑动切换页面

    1.实现思路 主界面四个导航按钮使用RadioButton,通过Selector 设置它的drawableTop属性来设置所显示的图片.通过 FragmentPagerAdapter 实现切换. 2. ...

  2. (三)ajax请求不同源之nginx反向代理跨域

    一.基本原理 nginx是一个高性能的web服务器,常用作反向代理服务器.nginx作为反向代理服务器,就是把http请求转发到另一个或者一些服务器上. 用nginx反向代理实现跨域,是最简单的跨域方 ...

  3. CF 960 G

    难受的1b,怎么会这样 先去学写一发 NTT 大概说一下斯特林数

  4. 根据文件大小自动判断单位B,KB,MB,GB

    <php> /** * 文件大小格式化 * @param integer $size 初始文件大小,单位为byte * @return array 格式化后的文件大小和单位数组,单位为by ...

  5. 移动端滑动效果 swiper 4.0.7

    <!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...

  6. Qt写websocketpp服务端

    1.下载websocketpp,地址为https://github.com/zaphoyd/websocketpp,版本为0.7. 2.下载boost,地址为https://www.boost.org ...

  7. maven和glassfish安装和部署及hello1和hello2的部署

    1.安装maven和glassfish及配置环境变 首先搜索并下载maven3.6.0和glassfish4.1.1(版本看按需要选择). 点击安装包进行安装 安装完成后开始配置环境变量 打开系统环境 ...

  8. iniReader,读取配置文件中数据

    #include ”IniOperation.h" CString strPath = strIntancePath + _T("..\\config.ini"); // ...

  9. raycast 一小段距离碰撞到的poly

    dtNavMeshQuery::raycast(dtPolyRef startRef, const float* startPos, const float* endPos, const dtQuer ...

  10. java_基础_接口和抽象类

    1.接口 java中接口的存在意义是:让多个继承该接口的类实现多态,让多个类有相同的特征 示例代码: interface MyInterface{ void myMethod(); } class T ...