如何将SQL查询更改为Linq

时间:2014-09-25 07:40:50

标签: sql sql-server linq

将复杂的sql查询转换为linq格式时遇到了一些困难。 这是我在sql server上的原始SQL查询:

select DriverID, DriverName, DriverCode,
count(*), 
sum(case IsDelivered  when 1 then 1 else 0 end),
sum(case IsDelivered when 0 then 1 else 0 end)
from tb_DriverContractDeliveries where DriverName!='' and DeliveryDate = '20140925' group by DriverID, DriverName, DriverCode , DeliveryDate order by DriverName asc

我可以将它转换为linq查询为bleow:

var result = (from n in db.tb_DriverContractDeliveries
                          where n.DriverName != "" && n.DeliveryDate == date
                          orderby n.DriverName
                          group n by new { n.DriverID, n.DriverName, n.DriverCode } into g
                          select new
                          {
                              DriverID = g.Key.DriverID,
                              DriverName = g.Key.DriverName,
                              DriverCode = g.Key.DriverCode,
                              Total = g.Count(),
                              Delivered = g.Count(n => n.IsDelivered.Equals("1")),
                              Remaining = g.Count(n => n.IsDelivered.Equals("0"))
                          }).ToList();

但是,我在sql语句中添加了一个新的子查询,它在sql server上执行得很好,但我不知道如何将这个块转换为linq:

select DriverID, DriverName, DriverCode,
count(*), 
sum(case IsDelivered  when 1 then 1 else 0 end),
sum(case IsDelivered when 0 then 1 else 0 end),
Substring(convert(varchar(30),(select top 1 EstTime from tb_DriverContractDeliveries bar 
where 
    bar.DriverID = tb_DriverContractDeliveries.DriverID 
and 
    bar.DeliveryDate = tb_DriverContractDeliveries.DeliveryDate 
and 
    max(tb_DriverContractDeliveries.DeliveredTime) = bar.DeliveredTime) ,20),12,5)
from tb_DriverContractDeliveries where DriverName!='' and DeliveryDate = '20140925' group by DriverID, DriverName, DriverCode , DeliveryDate order by DriverName asc

我自己完成了这个工作,linq查询应该如下:

var results = (from n in db.tb_DriverContractDeliveries
                       where n.DriverName.Equals("") == false && n.DeliveryDate == DateTime.Today
                       orderby n.DriverName
                       group n by new { n.DriverID, n.DriverName, n.DriverCode } into g
                       select new
                       {
                           DriverID = g.Key.DriverID,
                           DriverName = g.Key.DriverName,
                           DriverCode = g.Key.DriverCode,
                           Total = g.Count(),
                           Delivered = g.Count(n => n.IsDelivered.Equals("1")),
                           Remaining = g.Count(n => n.IsDelivered.Equals("0")),
                           EstTime = (from n in db.tb_DriverContractDeliveries
                                      where n.DriverID == n.DriverID && n.DeliveryDate == n.DeliveryDate && g.Max(x => x.DeliveredTime) == n.DeliveredTime
                                      select n.EstTime).FirstOrDefault().ToString()
                       });

1 个答案:

答案 0 :(得分:1)

请参阅以下示例查询。希望这对你有用。

  var secondselected = (from driver in lst
                        where driver.DriverName != "" && driver.DeliveryDate == Convert.ToDateTime("2014-05-01")
                        group driver by new { driver.DriverID, driver.DriverName, driver.DriverCode , driver.DeliveryDate} into drivergroup
                        select new
                        {
                            DriverID = drivergroup.Key.DriverID,
                            DriverName = drivergroup.Key.DriverName,
                            DriverCode = drivergroup.Key.DriverCode,
                            Total = drivergroup.Count(),
                            Delivered = drivergroup.Count(n => n.IsDelivered.Equals(true)),
                            Remaining = drivergroup.Count(n => n.IsDelivered.Equals(false)),
                            EstTime = (from dr in lst  
                            where dr.DriverID == drivergroup.Key.DriverID  
                                   && dr.DeliveryDate == drivergroup.Key.DeliveryDate 
                                   && dr.DeliveredTime  == drivergroup.Max(n => n.DeliveredTime)
                            select dr.EstTime).ToList().First(1)





                        }).ToList();

注意:lst是IEnumerable对象,即tb_DriverContractDeliveries对象