2014-03-24 48 views
0

可以有人請幫我一個SQL,我只是無法讓我的頭。在T-SQL,它看起來像這樣:LINQ - 左參數與參數

SELECT v.errandtype 
, COUNT(s.IVRTaskType) 
FROM V_ERRANDS v 
LEFT OUTER JOIN StatisticsRequest s 
    ON s.IVRTaskType = V.IVRTaskType 
    AND s.RegistrationDate >= '2014-03-24 00:00:00.000' 
    AND s.RegistrationDate <= '2014-03-24 23:59:59.000' 
    AND s.CountryID = 0 
GROUP BY v.errandtype 

但在LINQ ......那語言......所以嚇壞奇怪,我只是不明白這一點。

非常感謝這方面的幫助。

+0

看看[這個](http://www.thinqlinq.com/Post.aspx/Title/Left-Outer-Joins-in-LINQ-with-Entity-Framework)可以幫助LINQ中的左連接。 –

回答

0

在LINQ你的要求將是這樣的:

var statisticsRequest = yourContext.StatisticsRequest.Where(s => 
    s.RegistrationDate >= new DateTime(2014,03,24) && 
    s.RegistrationDate <= new DateTime(2014,03,25) && 
    s.CountryID == 0); 

var results = 
    from v in yourContext.V_ERRANDS 
    group v by v.errandtype into g 
    join s in statisticsRequest on s.IVRTaskType = g.FirstOrDefault().IVRTaskType 
    select new { 
     s.IVRTaskType, 
     g.Count() 
    } 
+0

感謝您的幫助! – user3456202

0
  • DefaultIfEmpty的左連接
  • DateTime變量LINQ查詢外
  • 匿名類型分組,這樣你就可以算其他桌子列

DateTime start = new DateTime(2014,03,24); 
DateTime end = new DateTime(2014,03,25); 

var query = from v in context.V_ERRANDS 
      from s in context.StatisticsRequest.Where(x => x.IVRTaskType == v.IVRTaskType) 
               .Where(x => x.RegistrationDate >= start && x.RegistrationDate < end) 
               .Where(x => x.CountryID == 0) 
               .DefaultIfEmpty() 
      group new { v, s} by v.errandtype into g 
      select new 
      { 
       errandtype = g.Key, 
       count = g.Select(x => x.s.IVRTaskType).Count(), 
      }; 
+0

謝謝!這工作完美。 – user3456202