簡體   English   中英

如何在LINQ中執行以下操作

[英]How to do the following in LINQ

我有以下SQL,如何在LINQ中實現這一點(請c#:)

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

我有以下內容(使用SubSonic 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 

我現在不知道如何添加a.EffectiveDate <b.EffectiveDate

如果您不加入,則無需重新分組。 如果您不加入,則不必有一個有趣的on子句。

您要做的就是過濾,所以您要做的只是在“哪里”。

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;

我建議您為查詢創建SP(存儲過程),並將該SP包含在DBML文件中,以便您可以從DataContext對象訪問/調用它作為方法。

在所有代碼供人類理解之后,保持盡可能簡單:)

單擊此處以獲取更多有關“如何在LINQ中調用存儲過程”的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM