代码之家  ›  专栏  ›  技术社区  ›  Podge

如何在LINQ中执行以下操作

  •  2
  • Podge  · 技术社区  · 16 年前

    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
    

    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.当前生效日期

    2 回复  |  直到 16 年前
        1
  •  0
  •   Amy B    16 年前

    如果你不加入,你就不必重组。 如果你不加入,你就不必开玩笑了。

    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
  •  -1
  •   Prashant Cholachagudda    16 年前

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

    毕竟代码是人类可以理解的,keep尽可能简单:)

    点击此处了解更多 "How to call Stored procedure in LINQ"