我有以下 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
最佳答案
不加入就不用重新组队。 如果您不加入,则不必在条款上搞笑。
您要做的就是过滤,所以您应该做的就是“在哪里”。
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;
关于c# - 如何在 LINQ 中执行以下操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1158236/