【问题标题】:How to do the following in LINQ如何在 LINQ 中执行以下操作
【发布时间】:2009-07-21 09:47:18
【问题描述】:

我有以下 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

【问题讨论】:

  • 至少让我们看看你到目前为止得到了什么,否则我们最终只会为你做所有的工作而什么都没学到。
  • 我有以下(使用 SubSonic Linq)var query = from a in List.All() join b in List.All() on new {a.Value,a.Category ,a.Description ,a.Item} 等于 new {b.Value,b.Category ,b.Description,b.Item} 到 temp 我不知道如何添加 a.EffectiveDate

标签: c# sql linq


【解决方案1】:

如果您不加入,则无需重新组合。 如果你不加入,你不必有一个有趣的 on 子句。

你要做的就是过滤,所以你应该做的就是'where'。

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;

【讨论】:

    【解决方案2】:

    我建议您为您的查询创建 SP(存储过程)并将该 SP 包含在 DBML 文件中,这样您就可以从 DataContext 对象中访问/调用它作为方法。

    毕竟代码是供人类理解的,所以保持尽可能简单:)

    点击这里了解更多"How to call Stored procedure in LINQ"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      相关资源
      最近更新 更多