资源描述:
《linqtoobject基本用法》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、LinQToObject基本用法.Linq的基本用法:Sort ,OrderBy,Skip,Take,Where,Compare,Join,Distinct,InsertRange等关键词Select用法 varselectedItems=fromiteminitemswhereitem.ParentID==parentIDorderbyitem.SortIndex descending,item.Nameascending selectitem;0.1where:varlist=collection.Where(t=>(txtCustomerNa
2、me.Text.Trim().Length==0
3、
4、t.ClientName.ToUpper().IndexOf(txtCustomerName.Text.Trim().ToUpper())>=0)); //根据条件查询,如果txtCustomerName中有值则匹配collection中的ClientName是否包含这个txtCustomerName的值10.1LInq递归实现定义function privateBuildExpression(IEnumberableenumberableList){...}returnfa
5、ctory=> { //递归实现的function BuildExpression(factory); };一、LinQToObject主要使用在一些array,list,collection,IEnumerable等数组上面,varlist=newList(); varlist2=newList();1.01list.Select(t=>t.CreateDate).SeperateToString(","); //selectcreatedate字段集合并组
6、成string返回1.02 list.Select(t=>newDataEntity{Name=t.Name,Value=t.Value}); //selectTypeOf(list)类型中的某些字段到新的DataEntity实例中1.11list.OrderBy(entity=>entity.CreateDate); //entity表示T的一个实例,按照createdate顺序排列,反之则使用listOrderByDescing1.12selectedItems.OrderBy(entity=>entity.SortIndex).ThenB
7、y(entity=>entity.name); //多个字段排序1.2 list.Sort( (x,y)=>StringComparer.CurrentCultureIgnoreCase.Compare(x.CreateDate,y.CreateDate));//x,y表示T的一个实例,x在y前面表示顺序排列,如果变为Compare(y.CreateDate,x.CreateDate)表示倒序排列1.3list.Skip(count) //count表示跳过count个数据 处理分页可以使用list.Skip((page-1)*pageSize)
8、.Take(pageSize);1.4list.Take(count) //count表示选取count个数据实例1(按照createdate排序,并选取前n个T类型的集合):list.OrderBy(entity==>entity.CreateDate).Take(n).ToList();1.5list.Distinct(); //删除重复项,list必须为一维数据组1.6list.Count(); //list数字的记录条数1.6.1 list.Count(item=>item.Name=='test') //查询list中name为t
9、est的记录条数1.7 list.Sum(); //合计,list必须为一维数组1.7.1 list.Sum(item=>item.Quantity); //合计,合计list中的quantity字段1.8list.Min(); //list中的最小值或记录1.8.1 list.Select(item=>item.Quantity).Min() //或者list中数量最少的记录1.8.2 list.Min(item=>item.Quantity) //或者list中数量最少的记录1.8list.InsertRange(0,list2)
10、//在list的指定位置插入list21.9list.ForEach(item=>item.Quantity