c# - 如何在 LINQ 中执行以下操作

标签 c# sql 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

最佳答案

不加入就不用重新组队。 如果您不加入,则不必在条款上搞笑。

您要做的就是过滤,所以您应该做的就是“在哪里”。

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/

相关文章:

c# - 如何在客户端和服务器端设置jsonp

MySQL按字母和数字列排序

sql - 基于多个字段对集合中的记录进行排名

sql - 在没有管理员权限的情况下运行 xp_create_subdir

c# - 检测到循环引用 Linq

c# - 'List' 不包含 'Where' 的定义

c# - EF5 和 Lambda/Linq - 如何仅包含相关表的某些列

c# - 如何中断事件处理程序

c# - 真实世界的异步和等待代码示例

c# - 如何使 DataGrid 在 WPF 中始终显示其列标题?