Linq to SQL没有产生任何结果

时间:2017-04-28 03:59:36

标签: c# entity-framework linq

您好我想知道为什么这个linq to sql语句没有产生任何结果?

请参阅下面的代码

sql语句

select MAX(sop.EndDate), ADR.Address1, itm.Name, sum(sop.MadeCount) from SnapshotOrderPositionHistories sop
inner join Addresses adr on adr.id = sop.Address_Id
inner join Items itm on itm.Id = sop.Item_Id
where adr.TraderId = 11 and sop.EndDate >= '2017-04-01 00:00:00.0000000' and sop.EndDate <= '2017-04-30 00:00:00.0000000' GROUP BY adr.Address1, itm.Name

linq声明

    var getDeliveriesByAddress = from snapShots in db.SnapshotOrderPositionsHistories
                                  join addresses in db.Addresses on snapShots.Address_Id equals addresses.Id
                                  join itms in db.Items on snapShots.Item_Id equals itms.Id
                                  where addresses.TraderId == financialDocument.Trader.Id && (snapShots.EndDate >= financialDocument.StartDate & snapShots.EndDate <= financialDocument.EndDate)
                                  group new { addresses, itms } by new { addresses.Address1, itms.Name} into QueryList
                                 select new { List = QueryList };

我正在使用实体框架

1 个答案:

答案 0 :(得分:1)

你错过了&&在哪里,并且投影不正确。你需要这个:

select new 
{ 
    MaxEndDate = QueryList.Max(q=>q.snapShots.EndDate),
    Address1 = QueryList.Key.Address1,
    Name = QueryList.Key.Name ,
    TotalMadeCount = QueryList.Sum(q=>q.snapShots.MadeCount) 
};

<强>更新

我再次写了查询,以显示allignment和namings的重要性。实际上,关于这些命名,我只更改了QueryListgr,这在组的上下文中更有意义,而不是QueryList

var getDeliveriesByAddress = from snapShots in db.SnapshotOrderPositionsHistories
                             join addresses in db.Addresses 
                             on snapShots.Address_Id equals addresses.Id
                             join itms in db.Items 
                             on snapShots.Item_Id equals itms.Id
                             where addresses.TraderId == financialDocument.Trader.Id 
                                && snapShots.EndDate >= financialDocument.StartDate 
                                && snapShots.EndDate <= financialDocument.EndDate
                             group new { addresses, itms } 
                             by new { addresses.Address1, itms.Name} into gr
                             select new 
                             {  
                                 MaxEndDate = gr.Max(q=>q.snapShots.EndDate),
                                 Address1 = gr.Key.Address1,
                                 Name = gr.Key.Name ,
                                 TotalMadeCount = gr.Sum(q=>q.snapShots.MadeCount) 
                             };

同样适用于SQL查询。

SELECT 
    MAX(sop.EndDate)
    , ADR.Address1
    , itm.Name
    , SUM(sop.MadeCount) 
FROM SnapshotOrderPositionHistories sop
INNER JOIN Addresses adr 
   ON adr.id = sop.Address_Id
INNER JOIN Items itm ON itm.Id = sop.Item_Id
WHERE 
    adr.TraderId = 11 
AND sop.EndDate >= '2017-04-01 00:00:00.0000000' 
AND sop.EndDate <= '2017-04-30 00:00:00.0000000' 
GROUP BY adr.Address1, itm.Name