日期:2014-05-20  浏览次数:20745 次

求解决!!
select sGuid,
max(case dName when '人事部' then dName else '' end) as hr,
max(case dName when '临时部门' then dName else '' end)as temp,
max(case dName when '财务部' then dName else '' end) as mony
from (
select Staff.Guid as sGuid,isCharge,(Department.Name)dName from Staff
inner join adminType on Staff.Stationid=adminType.ID
inner join admin_Deptadmin on Staff.Code = admin_Deptadmin.loginID 
inner join Department on admin_Deptadmin.DeptId= Department.Id
where adminType.Level=1 and Staff.bureauid=1)a group by sGuid

将以上sql语句转换成linq语句 实现相同的效果,求高手解决!!

------解决方案--------------------
C# code

var query=from a in (
from sf in Staff
join at in adminType on sf.Stationid equals ad.ID
join ad in admin_Deptadmin on sf.Code equals ad.loginID
join dp in Department on ad.DeptId equals dp.Id
where at.Level==1 && sf.bureauid==1
select new 
{
  sGuid=sf.Guid,
   isCharge=sf.isCharge,
   dName=dp.Name
})
group a by a.sGuid into g
select new 
{
  sGuid=g.Key,
  hr=g.Where(m=>m.dName=="从事部").Max(),
  temp=g.Where(m=>m.dName=="临时部门").Max(),
  mony=g.Where(m=>m.dName=="财务部").Max()
};

------解决方案--------------------
hr=g.Where(m=>m.dName=="从事部").Select(m=>m.dName).Max(),
temp=g.Where(m=>m.dName=="临时部门").Select(m=>m.dName).Max(),
mony=g.Where(m=>m.dName=="财务部").Select(m=>m.dName).Max()

只提供思路参考