test
classProgram
{
#region 示例1:不使用LINQ查询数组
staticvoidMain(string[] args)
{
int[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };
List<int> list=newList<int>();
foreach (intiteminnums)
{
if (item%2!=0)
list.Add(item);
}
list.Sort();
list.Reverse();
foreach (intiteminlist)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
#endregion
#region 示例2:使用LINQ技术查询数组
staticvoidMain(string[] args)
{
int[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };
varlist=fromnuminnums
wherenum%2!=0
orderbynumdescending
selectnum;
foreach (intiteminlist)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
#endregion
#region 示例3:扩展方法Select()应用
staticvoidMain(string[] args)
{
int[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };
varlist=nums.Select(item=>item*item);
foreach (intiteminlist)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
#endregion
#region 示例4:扩展方法Where()应用
staticvoidMain(string[] args)
{
int[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };
varlist=nums
.Where(item => item % 2 == 0)
.Select(i => i * i);
Console.ReadLine();
}
#endregion
#region 示例5:扩展方法OrderBy()应用
staticvoidMain(string[] args)
{
int[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };
varlist=nums
.Where(item => item % 2 == 0)
.Select(i => i * i)
.OrderBy(item => item);
foreach (intiinlist)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
staticvoidMain(string[] args)
{
string[] nums= { "张勇", "王琦", "刘静", "赵鑫鑫",
"杜丽", "马俊才", "那英", "成龙", };
varlist=nums
.Where(item => item.Length == 2)
.Select(item => item)
.OrderByDescending(item => item.Substring(0, 1));
foreach (stringiteminlist)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
#endregion
#region 示例6:扩展方法GroupBy()应用
staticvoidMain(string[] args)
{
string[] nums= { "张勇", "王琦", "刘静", "赵鑫鑫",
"杜丽", "马俊才", "那英", "成龙","王丽", "杜宇","马晓","刘丽","马大哈",};
varlist=nums
.Where(item => item.Length == 2)
.Select(item => item)
.GroupBy(item => item.Substring(0, 1));
foreach (vargroupIteminlist)
{
Console.WriteLine("-------------------");
Console.WriteLine("分组字段:{0}", groupItem.Key);
foreach (varitemingroupItem)
{
Console.WriteLine(item);
}
}
Console.ReadLine();
}
#endregion
#region 示例7:断点调试LINQ的查询时机
staticvoidMain(string[] args)
{
int[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };
varlist=nums
.Where(item => item % 2 == 0)
.Select(item => item * item)
.OrderBy(item => item);
foreach (intiinlist)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
#endregion
#region 示例8:查询的立即执行
staticvoidMain(string[] args)
{
int[] nums= { 1, 7, 2, 6, 5, 4, 9, 13, 20 };
varlist=nums
.Where(item => item % 2 == 0)
.Select(item => item * item)
.OrderBy(item => item)
.Count();
Console.WriteLine(list.ToString ());
Console.ReadLine();
}
#endregion
#region 示例9:from子句的简单使用
staticvoidMain(string[] args)
{
ArrayListvalues=newArrayList();
for (inti=0; i<10; i++) { values.Add(i); }
varlist=fromintiteminvalues
whereitem%2==0
selectitem;
foreach (intiteminlist) { Console.WriteLine(item); }
Console.ReadLine();
}
#endregion
#region 示例10:复合from子句的使用
staticvoidMain(string[] args)
{
Studentobj1=newStudent()
{
StuId=1001,
StuName="学员1",
ScoreList=newList<int>() { 90, 78, 54 }
};
Studentobj2=newStudent()
{
StuId=1002,
StuName="学员2",
ScoreList=newList<int>() { 95, 88, 90 }
};
Studentobj3=newStudent()
{
StuId=1003,
StuName="学员3",
ScoreList=newList<int>() { 79, 76, 89 }
};
将学员封装到集合中
List<Student> stuList=newList<Student>() { obj1, obj2, obj3 };
查询成绩包含95分以上的学员
varresult=fromstuinstuList
fromscoreinstu.ScoreList
wherescore>=95
selectstu;
显示查询结果
foreach (variteminresult)
{
Console.WriteLine(item.StuName);
}
Console.ReadLine();
}
#endregion
#region 示例11:多个from子句查询的使用
staticvoidMain(string[] args)
{
Studentobj1=newStudent() { StuId=1001, StuName="学员1" };
Studentobj2=newStudent() { StuId=1009, StuName="学员9" };
Studentobj3=newStudent() { StuId=1012, StuName="学员12" };
Studentobj4=newStudent() { StuId=1003, StuName="学员3" };
Studentobj5=newStudent() { StuId=1019, StuName="学员19" };
Studentobj6=newStudent() { StuId=1006, StuName="学员6" };
List<Student> stuList1=newList<Student>() { obj1, obj2, obj3 };
List<Student> stuList2=newList<Student>() { obj4, obj5, obj6 };
查询学好大于1010的学员
varresult=fromstu1instuList1
wherestu1.StuId>=1010
fromstu2instuList2
wherestu2.StuId>=1010
selectnew { stu1, stu2 };
显示查询结果
foreach (variteminresult )
{
Console.WriteLine(item.stu1.StuName+" "+item.stu1.StuId);
Console.WriteLine(item.stu2.StuName+" "+item.stu2.StuId);
}
Console.ReadLine();
}
#endregion
#region 示例12:聚合函数Count
staticvoidMain(string[] args)
{
Studentobj1=newStudent() { StuId=1001, StuName="学员1" };
Studentobj2=newStudent() { StuId=1009, StuName="学员9" };
Studentobj3=newStudent() { StuId=1012, StuName="学员12" };
Studentobj4=newStudent() { StuId=1003, StuName="学员3" };
Studentobj5=newStudent() { StuId=1019, StuName="学员19" };
Studentobj6=newStudent() { StuId=1006, StuName="学员6" };
List<Student> stuList=newList<Student>() { obj1, obj2, obj3, obj4, obj5, obj6 };
varcount1= (fromcinstuList
wherec.StuId>1010
selectc).Count();
varcount2=stuList.Where(c=>c.StuId>1010).Count();
Console.WriteLine("count1={0} count2={1}",count1,count2);
Console.ReadLine();
}
#endregion
#region 示例13:聚合函数Max、Min、Average
staticvoidMain(string[] args)
{
Studentobj1=newStudent() { StuId=1001, Age=22, StuName="学员1" };
Studentobj2=newStudent() { StuId=1009, Age=21, StuName="学员9" };
Studentobj3=newStudent() { StuId=1012, Age=25, StuName="学员12" };
Studentobj4=newStudent() { StuId=1003, Age=23, StuName="学员3" };
Studentobj5=newStudent() { StuId=1019, Age=27, StuName="学员19" };
Studentobj6=newStudent() { StuId=1006, Age=24, StuName="学员6" };
List<Student> stuList=newList<Student>() { obj1, obj2, obj3, obj4, obj5, obj6 };
varmaxAge= (fromsinstuList
selects.Age).Max();
varminAge=stuList
.Select(s => s.Age).Min();
varavgAge= (fromsinstuList
selects.Age).Average();
varsumAge= (fromsinstuList
selects.Age).Sum();
Console.WriteLine("maxAge={0} minAge={1} avgAge={2} sumAge={3}",
maxAge, minAge, avgAge,sumAge);
Console.ReadLine();
}
#endregion
#region 示例14:排序类ThenBy的使用
staticvoidMain(string[] args)
{
Studentobj1=newStudent() { StuId=1001, Age=22, StuName="学员1" };
Studentobj2=newStudent() { StuId=1009, Age=21, StuName="学员9" };
Studentobj3=newStudent() { StuId=1012, Age=25, StuName="学员12" };
Studentobj4=newStudent() { StuId=1003, Age=23, StuName="学员3" };
Studentobj5=newStudent() { StuId=1019, Age=27, StuName="学员19" };
Studentobj6=newStudent() { StuId=1006, Age=24, StuName="学员6" };
List<Student> stuList=newList<Student>() { obj1, obj2, obj3, obj4, obj5, obj6 };
varstus1=fromsinstuList
orderbys.StuName, s.Age, s.StuId
selects;
varstus2=stuList
.OrderBy(s => s.StuName)
.ThenBy(s => s.Age)
.ThenBy(s => s.StuId)
.Select(p => p);
foreach (varsinstus1)
{
Console.WriteLine(s.StuName);
}
Console.WriteLine("----------------------");
foreach (varsinstus2)
{
Console.WriteLine(s.StuName);
}
Console.ReadLine();
}
#endregion
#region 示例15:分区类查询
staticvoidMain(string[] args)
{
int[] nums= { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
varlist1=nums.Skip(1).Take(3);
varlist2=nums.SkipWhile(i=>i%3!=0)
.TakeWhile(i => i % 2 != 0);
foreach (variteminlist1) { Console.WriteLine(item); }
Console.WriteLine("------------");
foreach (variteminlist2) { Console.WriteLine(item); }
Console.ReadLine();
}
#endregion
#region 示例16:集合类查询Distinct
staticvoidMain(string[] args)
{
int[] nums= { 1, 2, 2, 6, 5, 6, 7, 8, 8 };
varlist=nums.Distinct();
foreach (variteminlist) { Console.WriteLine(item); }
Console.ReadLine();
}
#endregion
#region 示例17:生成类查询
staticvoidMain(string[] args)
{
varnums1=Enumerable.Range(1, 10);
varnums2=Enumerable.Repeat("LINQ best!", 10);
foreach (variteminnums1) { Console.WriteLine(item); }
Console.WriteLine("------------");
foreach (variteminnums2) { Console.WriteLine(item); }
Console.ReadLine();
}
#endregion
}
留言