首页 > 代码库 > LINQ 操作符(二)
LINQ 操作符(二)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lambda表达式
{
class Program
{
static void Main(string[] args)
{
}
static List<Person> GetPersonList()
{
return new List<Person>() {
new Person (){ Age =1,Name ="小白01",ID=1,Gender =true},
new Person (){Age =2,Name ="小白02",ID=2,Gender =false},
new Person (){Age =3,Name ="小白03",ID=3,Gender =true}
};
}
static List<PersonToy> GetPersonToyList()
{
return new List<PersonToy>() {
new PersonToy(){ PersonToyID=1,Name="小明"},
new PersonToy (){PersonToyID =2,Name ="小白"},
new PersonToy (){PersonToyID =3,Name ="小红"},
new PersonToy (){PersonToyID =4,Name ="小黑"}
};
}
#region Lambda表达式
/// <summary>
/// Lambda表达式和Lambda语句
/// </summary>
static void TestLambda()
{
List<Person> list = GetPersonList();
list.ForEach(d => Console.WriteLine(d.Name));
//Lambda表达式
list.FindAll(d => d.Age > 1);
list.Sort((x, y) => x.Age - y.Age);
var it = list.Select(p => new smallperson { Name = p.Name });
//语句Lambda语句
var it1 = list.Select(p =>
{ //如果存在多个语句就不能省略{}大括号 也不能省略return
smallperson sp = new smallperson();
sp.Name = p.Name;
return sp;
});
}
#endregion
/// <summary>
/// 标准查询方法 ----Where查询
/// </summary>
static void SQOWhere()
{
List<Person> list = GetPersonList();
//在集合中筛选出符合条件的数据
List<Person> list2 = list.Where(p => p.Gender == false && p.Age > 1 && p.Age < 3).ToList();
list.ForEach(p => Console.WriteLine(p.Name));
}
/// <summary>
/// 标准查询运算符 select 投射方法(返回一个新的集合)
/// </summary>
static void SQOSelect()
{
List<Person> list = GetPersonList();
List<smallperson> list2 = list.Where(p => p.Gender == false).Select(p => new smallperson() { Name = p.Name }).ToList();
}
/// <summary>
/// 标准查询运算符 Order 排序方法 返回一个新的集合
/// </summary>
static void SQOOrder()
{
List<Person> list = GetPersonList();
List<Person >listSorted= list.OrderBy(p => p.Age).ThenByDescending(p=>p.ID ).ToList ();//按照年龄进行排序(升序)
// list .OrderByDescending 倒序
list.ForEach(p=>Console .WriteLine(p.Age.ToString ()));
}
/// <summary>
/// 标准运算符--连接集合
/// </summary>
static void TestJoin()
{
List<Person> listperson = GetPersonList();
List<PersonToy> listPersontToy = GetPersonToyList();
var listJoin= listperson .Join (listPersontToy,d=>d.ID,t=>t.PersonToyID,(d,t)=>new {ID=d.ID ,ToyName=t.Name,Name=d.Name}).ToList ();
listJoin.ForEach(a=>Console .WriteLine (a.ID.ToString ()+","+a.ToyName));
}
/// <summary>
/// 标准查询运算符 GroupBy为集合分组
///
/// </summary>
static void SQOGroupBy()
{
List<Person> list = GetPersonList();
//按照性别进行分组
IEnumerable <IGrouping <bool ,Person >>it= list.GroupBy(p => p.Gender);
//根据姓名进行分组
IEnumerable<IGrouping<string, Person>> it2 = list.GroupBy(p=>p.Name );
// var it2= list.GroupBy(p=>p.Name );
IList<IGrouping<bool, Person>> listGroup = it.ToList();
foreach (var group in listGroup)
{
Console.WriteLine(group .Key.ToString ());
foreach (var item in group)
{
Console.WriteLine(item .Name +","+item .Age);
}
Console.WriteLine("------------------------------------");
}
}
/// <summary>
/// 标准运算符=Skip -Take分页数据
/// </summary>
static void SQOPagedList()
{
List<Person> list = GetPersonList();
}
/// <summary>
///Linq语句 ,编译后会生成对应的标准查询 运算符 linq是一种类似sql的一种更加友好的语法糖 语法而已
/// </summary>
static void TestLinq()
{
List<Person> list = GetPersonList();
List<PersonToy>listtoy=GetPersonToyList();
var query = from li in list select li;//从老集合中每一个元素 存入一个新的集合
//where
var query1 = from li in list where li.Gender == true select li;
//order descending倒序 aescending正序
var query2 = from li in list orderby li.Gender orderby li.Age descending select li;
//join链接查询
var query3=from li in list join t in listtoy on li .ID equals t.PersonToyID select new {Name=li .Name };
//分组查询
var query4 = from li in list group li by li.Gender;
}
}
public class smallperson
{
public string Name { get; set; }
}
}