c# linq查询

List<DeptInfo> deptList = (from emp in empList

where emp.Status == "在职" //筛选“在职”员工

orderby emp.DeptID ascending //按“部门ID”排序

group emp by new //按“部门ID”和“部门名称”分组

{

emp.DeptID,

emp.DeptName

}

into g

select new DeptInfo()

{

DeptID = g.Key.DeptID,

DeptName = g.Key.DeptName,

EmplayeeCount = g.Count(), //统计部门员工数量

WageSum = g.Sum(a => a.Wage), //统计部门工资总额

WageAvg = g.Average(a => a.Wage), //统计部门平均工资

EmplayeeList = (from e in g //归集部门员工列表

select new Emplayee()

{

EmpID = e.EmpID,

EmpName = e.EmpName

}

).ToList()

}).ToList();

拼接分组后的字符:

1:

var result = from item in tbl
 group item by item.name into g
let ids = g.Select(b => b.id.ToString()).ToArray()
 select new { id = String.Join(",", ids), name = g.Key };

:2:

var tbl2 = from item in db.tbl select new {id=item.id.ToString(),item.name };

var result = from item in tbl2

    group item by item.name into g

    select g.Aggregate(

        (workingSentence, next) => 

            new { 
ids =workingSentence.id + "," +next.id , name = g.Key
}
);

//分组 求和

var temp = (from a in student

from p in syncchip

// join p in syncchip

//on c.id equals p.user_id

where a.class_id == index

orderby a.id

group new {a.id,a.name,a.className,a.sex ,a.head_img, p.user_id }

by new { a.id, a.name, a.className, a.sex, a.head_img} into g

select new

{

stdent_id = g.Key.id,

name = g.Key.name,

className = g.Key.className,

sex = g.Key.sex,

head_img = g.Key.head_img,

cou= g.Count(c => c.user_id== g.Key.id)

}).ToList();

var temp = (from a in student

from p in syncchip

// join p in syncchip

//on c.id equals p.user_id

where a.class_id==index

orderby a.id

group a by new { a.id,a.name,a.className ,a.sex,a.head_img } into g

select new

{

stdent_id =g.Key.id,

name = g.Key.name,

className = g.Key.className,

sex = g.Key.sex,

head_img=g.Key.head_img,

cou = (from e in syncchip

where e.user_id == g.Key.id

group e by e.user_id into b

select new

{

cou = g.Count()

}

).Count()

}).ToList();

List<StudentClass> list = new List<StudentClass>();

foreach (var i in temp)

{

list.Add(new StudentClass {student_id =i.stdent_id,name=i.name,cou=i.cou ,className=i.className,sex=i.sex,head_img=i.head_img});

}

var temp = (from a in Waybills

from p in PlanOrders

where a.PlanId == p.Id

orderby a.SendTime

group a

by new { a.ProId ,a.ProName, a.GmId, a.GmName, a.GmModel, p.PlanNumber } into g

select new

{

Time= teme.ToString(),

Type="当日汇总",

ProId = g.Key.ProId,

Id= g.FirstOrDefault().Id,

PlanId = g.FirstOrDefault().PlanId,

ProName = g.Key.ProName,

GmId = g.Key.GmId,

GmName = g.Key.GmName,

GmModel = g.Key.GmModel,

//计划数量

PlanNumber=g.Key.PlanNumber,

//完成数量

Sum = g.Sum(a => a.LoadingWeight),

//完成率

CompletionRate = decimal.Round(((g.Sum(a => a.LoadingWeight) / g.Key.PlanNumber) * 100),2),

//派车数量

cou=g.Count(),

//完成车数

Complete = g.Count(a => a.Status==2)

//备注

}).ToList();