【问题标题】:LINQ: get access to child lists during runtimeLINQ:在运行时访问子列表
【发布时间】:2010-10-11 17:46:31
【问题描述】:

我进行分组并获取子列表... 在查询期间,我使用

创建了一个新 obj
var result3 = from tick in listTicks
              group tick by bla bla into g
              select new 
              { 
                  Count = g.Count(), 
                  Key = g.Key, 
                  Items = g,
                  Timestamp = g.First().timestamp,
                  LastTimestamp = g[-1].First().timestamp result3 isn't still declared???
              };

我想在运行时访问最后创建的 obj 的新值 也许检查最后一个 first.Timestamp 是否具有特定值

在创建 select new { } 期间是否可以访问最后一个 g 我想从最后一个 g 中检查一个实际值

我想像 result3[result.count - 1].timestamp 之类的东西???在选择新部分...

【问题讨论】:

    标签: c# linq group-by


    【解决方案1】:

    我不应该理解正确,但这就是你想要的吗?

    result3.Last().Timestamp;
    

    评论后:我想我现在明白了。 您需要创建一个临时变量来存储最后一组的时间戳并将其值设置为更复杂的委托:

    int lastTimestamp = 0; // Put the correct type and default value
    
    var result3 = (from tick in listTicks
                  group tick by bla bla into g
                  select g)
                  .Select
                  (g => 
                  {
                      // Create your object with the last timestamp
                      var result = new
                      { 
                          Count = g.Count(), 
                          Key = g.Key, 
                          Items = g,
                          Timestamp = g.First().timestamp,
                          LastTimestamp = lastTimestamp
                      };
                      // Set last timestamp for next iteration
                      lastTimestamp = result.Timestamp;
                      // Return your object
                      return result;
                  });
    

    不知道确切的上下文,但您可能想要添加“ToList()”来覆盖延迟获取。

    【讨论】:

    • 这不起作用...代码看起来不错...但在调试中... result3 没有来自临时结果 Counte、Key .... 等的新变量。 ..
    猜你喜欢
    • 2020-06-13
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 2011-01-05
    • 2012-03-30
    • 2010-09-19
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多