Linq To Object 函数介绍

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 转换一个继承IEnumerable的集合为IEnumerable<T>后执行其他linq操作 (延迟)
{
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 };
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
{
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
Console.ReadLine();
}

public class People : IEqualityComparer<People>
{
public string Name { get; set; }
public bool Equals(People x, People y)
{
return x.Name == y.Name;
}

public int GetHashCode(People obj)
{
return obj.Name.GetHashCode();
}
}

猜你喜欢

转载自www.cnblogs.com/WJHNET/p/9214719.html