C#——LINQ to object 基本用法(

Linq 的基本用法: Sort , OrderBy, Skip,Take,Where,Compare,Join,Distinct ,InsertRange 等关键词

Select用法var selectedItems = from item in items where item.ParentID == parentID orderby item.SortIndexdescending ,item.Name ascendingselect item;

一、 LinQ To Object 主要使用在 一些 array ,list,collection ,IEnumerable 等数组上面,

var list=new List<T>();var list2=new List<T>();

1.11 list.OrderBy(entity=>entity.CreateDate); //entity 表示 T的一个实例,按照 createdate 顺序排列

1.12 selectedItems.OrderBy(entity => entity.SortIndex).ThenBy(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.3 list.Skip(count) //count 表示 跳过count 个数据

1.4 list.Take(count) //count 表示 选取count 个数据

实例1(按照createdate 排序,并选取前 n个T 类型的集合):

list.OrderBy(entity==>entity.CreateDate).Take(n).ToList<T>();

1.5 list.Distinct(); //删除重复项,list 必须为一维数据组

1.6 list.Count(); //list 数字的记录条数

1.6.1 list.Count(item=>item.Name=='test') //查询list中 name 为 test 的记录条数

1.7 list.Sum(); //合计,list 必须为一维数组

1.7.1 list.Sum(item=>item.Quantity); //合计,合计list中的quantity 字段

1.8 list.Min(); //list 中的最小值或记录

1.8.1 list.Select(item=>item.Quantity).Min() //或者list中 数量最少的记录

1.8.2 list.Min(item=>item.Quantity) //或者list中 数量最少的记录

1.8 list.InsertRange(0,list2) //在list的指定位置插入list 2

 

二、以下内容

引用:http://hi.baidu.com/evans_wang/blog/item/961c1925fca65c34c89559dd.html

LINQ分页和排序,skip和Take 用法

2008年12月31日 星期三 10:20

 

dbconn.BidRecord.OrderBy(p=>p.bid_id).ToList<BidRecord>().OrderBy(p => p.bid_id).Skip(skip).Take(take).ToList<BidRecord>();

上面就是分页带排序的方法。

说一下问什么这样做

dbconn 是Modle的对象

BidRecord 是一个实体

P=〉p.bid_id 是排序的条件

OrderBy 是排序(后面是条件)然后要查询所有的,在所有的数据中在查询你要分页的数据,skip是在分页在前有多少条数据,也就是在当前页数据之前的数据总和,(跳过序列中指定数量的元素,然后返回剩余的元素。)pageSize*(pageIndex-1),Take 是返回多少条数据,也就是pageSize!

 

Linq 查询方法

public static bool SaveEmployeeToFunctionPoint(Guid functionPointID, List<Guid> employeeIDList){var oldCollection = new EmployeeToFunctionPointCollection();oldCollection.GetMulti(EmployeeToFunctionPointFields.FunctionPointId == functionPointID);var oldIDList = from item in oldCollection select item.EmployeeId;//选择 employeeidlist中不与 oldidlist 中重复的项var newList = from item in employeeIDList where !oldIDList.Contains(item) select item;var trans = new Transaction(System.Data.IsolationLevel.ReadCommitted, "SaveEmployeeToFunctionPoint");try{foreach (Guid empID in newList){var entity = new EmployeeToFunctionPointEntity{EmployeeId = empID,FunctionPointId = functionPointID,IsMain = false};trans.Add(entity);entity.Save();}trans.Commit();return true;}catch (Exception e){trans.Rollback();return false;}}

原文地址:https://www.cnblogs.com/zhoubinbin1991/p/2681589.html