c# - 如何使用此示例在 Linq 中动态旋转

标签 c# linq

下面的问题在代码中,即 DY1 =、DY2 = 等位。我不知道会有多少,所以我需要动态地做。

这是我的文本文件:

Product, Origin Year, Development Year, Incremental Value
Comp, 1992, 1992, 110.0
Comp, 1992, 1993, 170.0
Comp, 1993, 1993, 200.0
Non-Comp, 1990, 1990, 45.2
Non-Comp, 1990, 1991, 64.8
Non-Comp, 1990, 1993, 37.0
Non-Comp, 1991, 1991, 50.0
Non-Comp, 1991, 1992, 75.0
Non-Comp, 1991, 1993, 25.0
Non-Comp, 1992, 1992, 55.0
Non-Comp, 1992, 1993, 85.0
Non-Comp, 1993, 1993, 100.0

这是我的代码(从 LinqPad 粘贴):

void Main()
{
  using (var reader = new StreamReader("C:\\temp\\ExampleData.txt"))
  {
    var products =
        (from line in reader.Lines()
        where !line.StartsWith("Product")
        let parts = line.Split(',')
        select new {Product=parts[0], Origin_Year=parts[1], Development_Year=parts[2], Incremental_Value=parts[3], DY=int.Parse(parts[2]) - int.Parse(parts[1]) + 1 }).ToList();
    products.Dump();

    var grps = from p in products
    group p by new {p.Product, p.Origin_Year}
    into g
    select (new { g.Key.Product, g.Key.Origin_Year, 

    DY1 = g.Where(c => c.DY == 1).Select (c => c.Incremental_Value).FirstOrDefault(),
    DY2 = g.Where(c => c.DY == 2).Select (c => c.Incremental_Value).FirstOrDefault(),
    DY3 = g.Where(c => c.DY == 3).Select (c => c.Incremental_Value).FirstOrDefault(),
    DY4 = g.Where(c => c.DY == 4).Select (c => c.Incremental_Value).FirstOrDefault()    
    });

    grps.Dump();
  }
}

}// Temporary hack to enable extension methods

/// <summary>
/// Operators for LINQ to Text Files
/// </summary>
public static class Extensions
{
  public static IEnumerable<String> Lines(this TextReader source)
  {
    String line;

    if (source == null)
      throw new ArgumentNullException("source");

    while ((line = source.ReadLine()) != null)
      yield return line;
  }

这是我的输出:

Product   Origin_Year  DY1    DY2    DY3   DY4 
Comp      1992         110.0  170.0  null  null  
Comp      1993         200.0  null   null  null  
Non-Comp  1990         45.2   64.8   null  37.0  
Non-Comp  1991         50.0   75.0   25.0  null  
Non-Comp  1992         55.0   85.0   null  null  
Non-Comp  1993         100.0  null   null  null 

最佳答案

我能提供的最佳建议是为每个可能的 DY 返回一个值数组。

首先将 products 查询中的 Incremental_Value 字段修改为 double(即 double.Parse(parts[3] )).

现在为最大数量的 DY 创建一个枚举:

    var dys = Enumerable.Range(1, products.Max(p => p.DY));

grps 查询如下所示:

    var grps =
        from p in products
        group p by new
        {
            p.Product,
            p.Origin_Year,
        } into g
        let lookup = g.ToLookup(x => x.DY, x => x.Incremental_Value)
        select new
        {
            g.Key.Product,
            g.Key.Origin_Year, 
            DYs = dys.Select(x => lookup[x].Sum()).ToArray(),
        };

这对你有用吗?

关于c# - 如何使用此示例在 Linq 中动态旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6931481/

相关文章:

c# - 动态构建包含 Where 过滤器

c# - 类型转换泛型

c# - 如何从我的浏览器查看 soap 服务数据

c# - 如何在用户注销或关闭 Windows 时延迟关闭应用程序?

c# - 通过 LINQ 查询使用包含

C#/LINQ : Concatenating strings

c# - Page.RouteData.Values ["parameter"] 不工作

c# - 在 LINQ 之外使用匿名类型是一件好事吗?

c# - Linq Order by List<int> 给定的 ID

c# - 如何根据给定 xml 文件中的前一个节点获取值