2009-07-21 57 views
2

下面我有以下SQL,我怎麼在LINQ實現這個(C#請:))如何做LINQ

SELECT  a.Category, a.Description, a.Item, a.[Value], a.Loading, a.MaxDiscount, MIN(b.EffectiveDate) AS FromDate, a.EffectiveDate AS EndDate, a.SortOrder, a.ID 
FROM List a 
LEFT OUTER JOIN List b ON a.[Value] = b.[Value] AND a.Category = b.Category AND a.Description = b.Description AND a.Item = b.Item AND a.EffectiveDate < b.EffectiveDate 
GROUP BY a.Category, a.Description, a.Item, a.[Value], a.Loading, a.MaxDiscount, a.SortOrder, a.EffectiveDate, a.ID 
HAVING  (a.Category = 'General') AND (a.Description = 'Cover') AND (a.EffectiveDate <= CONVERT(DATETIME, '2009-04-01 00:00:00', 102)) AND (MIN(b.EffectiveDate) >= CONVERT(DATETIME, '2009-04-01 00:00:00', 102) OR MIN(b.EffectiveDate) IS NULL) AND (a.Item = 'Type') 
ORDER BY a.SortOrder 

我有以下的(使用亞音速LINQ)

var query = from a in List.All() join b in List.All() 
      on new {a.Value,a.Category ,a.Description,a.Item} 
      equals new {b.Value,b.Category ,b.Description,b.Item} into temp 

我不知道如何在此刻

+6

至少讓我們看看你到目前爲止,否則我們只是最終爲你做所有的工作,沒有什麼是學習的。 – leppie 2009-07-21 09:53:35

+1

我有以下(使用SubSonic Linq) var query = from a in List.All() join b in List.All()on new {a.Value,a.Category,a.Description,a。項目} 等於新{b.Value,b.Category,b.Description,b.Item} 到臨時 我不知道如何添加a.EffectiveDate Podge 2009-07-21 10:00:00

回答

0

如果你不加入,你不必重新組合一樣簡單。 如果你不加入,你不必有一個有趣的條款。

所有你想要做的就是過濾,所以你應該做的就是'在哪裏'。

someDate = new DateTime(2009, 4, 1); 

var query = 
from a in List 
where a.Category == "General" 
where a.Description == "Cover" 
where a.Item == "Type" 
where a.EffectiveDate < someDate 
let minDate = 
(
    from b in List 
    where a.Value == b.Value 
    where a.Category == b.Category 
    where a.Description == b.Description 
    where a.Item == b.Item 
    where a.EffectiveDate < b.EffectiveDate 
    orderby b.EffectiveDate 
    select new DateTime? (b.EffectiveDate) 
).FirstOrDefault() 
where !minDate.HasValue || someDate < minDate.Value 
orderby a.SortOrder 
select a; 
-1

我建議你爲你的查詢SP(存儲過程)添加a.EffectiveDate < b.EffectiveDate和包括SP在DBML文件中,你可以訪問/調用它作爲DataContext對象的方法。

畢竟代碼是人類理解,保持儘可能:)

點擊這裏查看更多關於"How to call Stored procedure in LINQ"