项目中写到看到的一些LINQ和Lambda语句

1,求和

       var datas = SellOutActualData.Where(b => b.BrandCode == brandExportParam.BrandInfo.BrandCode && b.ReportMonth == item.Month);
       decimal sumValue = datas.Sum(a => a.Amount);

2,Linq筛选,转换成指定类

var query = dbContext.TWSalesRecord.Where(d => d.DataSource == "HCT");
//筛选字段HCTAccount中的值要在 model.AccountList内
query = query.Where(d => model.AccountList.Contains(d.HCTAccount));

List<TWSalesData> data = query.Where(d => d.ReportMonth >= model.StartDate
                                       && d.ReportMonth <= model.EndDate
                                       && !string.IsNullOrEmpty(d.GSKGMMCode))
                              .Select(d => new TWSalesData
                                          {
                                              Account = d.HCTAccount,
                                              GMMCode = d.GSKGMMCode,
                                              ReportMonth = d.ReportMonth,
                                              Amount = d.GSKAmount
                                         }).ToList();           //将最终的结果转为Sales类集合

3,linq筛选,转换成 “字典”

private Dictionary<string, TWProducts> products = new Dictionary<string, TWProducts>();
products = db.TWProducts.ToDictionary(d => d.GMMCode, d => d);

4,分组查询

结果是,某个分销商,在某月中的 某个商品的 “总销售额” 和 “数量” 是多少

 List<SalesDataModel> sales = (from data in db.TWSalesRecord

                                          group data by new
                                          {
                                              data.HCTAccount,       //根据分销商,月份,商品编码分组
                                              data.ReportMonth,
                                              data.GSKGMMCode

                                          } into d
                                          select new SalesDataModel
                                          {
                                              Account = d.Select(s => s.HCTAccount).FirstOrDefault(),
                                              Month = d.Select(s => s.ReportMonth).FirstOrDefault(),
                                              GMMCode = d.Select(s => s.GSKGMMCode).FirstOrDefault(),
                                              Value = d.Sum(s => s.GSKAmount)/,
                                              Quantity = d.Sum(s => s.Quantity)
                                          }).ToList();

5,简单计算函数(sum,max,min,count)

//linq
Var ss=(from r in db.table select r).max(p=>p.id);
                                    .min(p=>p.id);
                                    .sum(p=>p.id);
                                    .count();
//Lambda
Var ss1=db.table.max(p=>p.id);
Var ss1=db.table.min(p=>p.id);
Var ss1=db.table.sum(p=>p.id);
Var ss1=db.table.count();
上一篇:ReactiveX 学习笔记(25)使用 RxJS + Vue.js 调用 REST API


下一篇:MUI --- 多个页面之间的传值 A页面B 页面 C页面