C# 常用linq、lambda表达式
1.在List中查询某元素,并返回另一个List List<string> listLoadMouldNo = listAllLoad.Select(m => m.MouldNo).ToList(); //查询出了listAllLoad中MouldNo元素,并放入另一个List. 2.筛选出List中符合条件的,放入另一个List List<DMDXSeg> lstSeg = listAllDMDX.Where(m => m.SegCode == 1012).ToList(); 3.找出List中某元素值包含于另一个List中的,放入另一个List List<DMDXSeg> list = listAllInv.Where(m => listAllMould.Contains(m.MouldNo)).ToList(); //找出listAllInv中MouldNo在listAllMould中存在的部分 4.去重,以List中某元素为基准,去除次元素相同的记录 List<MouldDMDXInfo> listBM_Distinct = listAllMouldBM.Where((m, i) => listAllMouldBM.FindIndex(z => z.MouldNo == m.MouldNo) == i).ToList(); //去除MouldNo相同的记录 5.以List中某元素为查询条件,判断List中是否存在数据 list.Exists(x => x.NGTYPE == 1); //判断List是否存在NGTYPE为1的元素 6.找出DataTable中interval最大的那条数据 DataRow[] oTmp = dt.Select("interval = max(interval)"); 7.以某字段为查询条件,查询DataTable中符合条件的数据 DataRow[] drs = dt.AsEnumerable().Where<DataRow>(m => m["name"].Equals("小明")).ToArray(); //找出DataTable中name为小明的数据 8、降序: lstroot.Sort((x,y)=>y.static_count.CompareTo(x.static_count)); 9、升序: lstroot.Sort((x,y)=>x.static_count.CompareTo(y.static_count));
10、排序之后再排序
items = AllProductItems.OrderBy(x => x.Barcode.Length).ThenBy(x => x.Barcode);
11、根据条件分组,并获取数量最大的分组
var ArrayList = MaterialPlanList
.Where(n => n.EveryDeliveryDate == Convert.ToDateTime(input.beiginDates).AddDays(i))
.GroupBy(x => new { x.SupplierName, x.MaterialNum })
.Select(x => new
{
SupplierName = x.Key.SupplierName,
MaterialNum = x.Key.MaterialNum,
Num = x.Count()
})
.OrderByDescending(x => x.Num)
.FirstOrDefault();
//工序名称
var processNameList = list2
.Join(list3, x => x.ProcessID, y => y.processId,
(x, y) => new processNameModel
{
EAI_WORKORDER_ID = x.EAI_WORKORDER_ID,
ProcessName = x.ProcessName,
ProcessID = x.ProcessID,
ProcessIndex = x.ProcessIndex,
processFlowId = y.processFlowId
}
)
.OrderBy(s => new { s.processFlowId, s.ProcessIndex })
.ToList();
//工序合格数量
var gxhgList = list2
.Where(x => x.TestResult == "0")
.GroupBy(x => new { x.ProcessID })
.Select(x => new
{
ProcessID = x.Key.ProcessID,
gxhg = x.Count<makeDatatModel>()
})
.ToList();
//工序完成数量
var gxwcList = list2
.GroupBy(x => new { x.ProcessID })
.Select(x => new
{
ProcessID = x.Key.ProcessID,
gxwc = x.Count<makeDatatModel>()
})
.ToList();
//左侧数据
var rightList = processNameList
.Join(gxhgList, x => x.ProcessID, y => y.ProcessID,
(x, y) => new
{
EAI_WORKORDER_ID = x.EAI_WORKORDER_ID,
ProcessName = x.ProcessName,
ProcessID = x.ProcessID,
gxhg = y.gxhg,
}
)
.Join(gxwcList, x => x.ProcessID, y => y.ProcessID,
(x, y) => new
{
EAI_WORKORDER_ID = x.EAI_WORKORDER_ID,
ProcessName = x.ProcessName,
ProcessID = x.ProcessID,
gxhg = x.gxhg,
gxwc = y.gxwc,
}
)
// .OrderBy(s => new { s.processFlowId, s.ProcessIndex })
.ToList();
Linq表达式: //按学号降序 List<Student> stuList = (from s instu orderby s.stuNOdescending select s).ToList<Student>(); //按学号升序 List<Student> stuList = (from s instu orderby s.stuNO select s).ToList<Student>(); 使用Lambda表达式排序: //按学号降序 单字段:List<Student> stuList= stu.OrderByDescending(s=> s.orderid).ToList<Student>(); 多字段:List<Student> stuList= stu.OrderByDescending(s=> new{s.stuNO,s.stuName}).ToList<Student>(); //按学号升序 单字段:List<Student> stuList= stu.OrderBy(s=> s.stuNO).ToList<Student>(); 多字段:List<Student> stuList= stu.OrderBy(s=> new{s.stuNO,s.stuName}).ToList<Student>(); 多字段主次顺序排序情况,先按no排序,再按name排序 List<Student> stuList= stu.OrderBy(s=> s.stuNO).ThenBy(s=> s.stuName).ToList<Student>(); List<Student> stuList= stu.OrderBy(s=> new{ s.stuNO }).ThenBy(s=> new{s.stuName}).ToList<Student>();
var TaskReportList = _plm_TaskReportRep.Entities.GroupBy(x => new { x.EXECUTORDEPARNAME, x.PROJECTPROP })
.Select(x => new
{
EXECUTORDEPARNAME = x.Key.EXECUTORDEPARNAME,
PROJECTPROP = x.Key.PROJECTPROP,
Num = x.Sum(x => x.PROJECTPROP)
}).ToList();