在 linq 中选择总小时数

发布于 2024-12-03 13:02:08 字数 945 浏览 0 评论 0原文

这很难解释,但我会尝试 我有一个包含一些字段的表,并且该表有包含行的子表(我不是在谈论 SQL,它是 DB4O,但我使用 linq)。行有 timefromtimeto,例如:

maintable:
Description,
CreateDate,
....
mainTableRows:
08:00,
09:00,
some description

现在我需要进行一个查询,对行中的小时数进行求和,如下所示:

09:00-08:00 = 1hour(first row) + 10:00-09:00 (second row)

等等。

类似于:

var item = from mainTable m 
           select new
             { 
               m.Id, 
               Hours = m.Rows.Sum(c=> new { Hour = c.TimeTo - c.TimeFrom }) 
             }

感谢大家的帮助,这是我的完整解决方案:

var item = from mainTable m 
               select new
                 { 
                   m.Id, 
                   Hours = String.Format("{0:HH:mm}", (new DateTime((p.TimesheetRows.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal += (t.TimeTo - t.TimeFrom)).Ticks))))
                 }

It will be hard to explain but I'll try
I've got a table with some fields, and that table got subtable with rows (I'm not talking about SQL, its DB4O, but I use linq). Rows got timefrom and timeto, example:

maintable:
Description,
CreateDate,
....
mainTableRows:
08:00,
09:00,
some description

Now I need to make a query that will sum hours from rows, like this:

09:00-08:00 = 1hour(first row) + 10:00-09:00 (second row)

and so on.

Something like:

var item = from mainTable m 
           select new
             { 
               m.Id, 
               Hours = m.Rows.Sum(c=> new { Hour = c.TimeTo - c.TimeFrom }) 
             }

Thanks all for help, this is mine full solution:

var item = from mainTable m 
               select new
                 { 
                   m.Id, 
                   Hours = String.Format("{0:HH:mm}", (new DateTime((p.TimesheetRows.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal += (t.TimeTo - t.TimeFrom)).Ticks))))
                 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-12-10 13:02:08

如果你想要总的经过时间,你可以这样做:

 var item = from mainTable m 
       select new
         { 
           m.Id, 
           Hours = m.Rows
                    .Select(c => c.TimeTo - c.TimeFrom)
                    .Aggregate( (working, next) => working.Add(next) )
                    .TotalHours
         }

If you want the total elapsed time, you can do:

 var item = from mainTable m 
       select new
         { 
           m.Id, 
           Hours = m.Rows
                    .Select(c => c.TimeTo - c.TimeFrom)
                    .Aggregate( (working, next) => working.Add(next) )
                    .TotalHours
         }
美人迟暮 2024-12-10 13:02:08

如果我理解正确的话,你就很接近了。选择之后才可以进行聚合。

private class TestData 
{
    public DateTime from;
    public DateTime to;
}

public static test()
{
   Collection<TestData> x = new Collection<TestData>();
   x.Select(item => (item.to - item.from).Hours).Sum();
}

If I understand you correctly you are pretty near. Only the aggregation could be made after the selection.

private class TestData 
{
    public DateTime from;
    public DateTime to;
}

public static test()
{
   Collection<TestData> x = new Collection<TestData>();
   x.Select(item => (item.to - item.from).Hours).Sum();
}
青萝楚歌 2024-12-10 13:02:08

假设 Rows 是某种 IEnumerable,如果您也想要小数小时,则可以这样做:

var item = from mainTable m 
           select new
             { 
               m.Id, 
               Hours = m.Row.Sum(c => (c.TimeTo - c.TimeFrom).TotalHours) 
             }

或者如果您想要整数小时的总和,则可以这样做:

var item = from mainTable m 
           select new
             { 
               m.Id, 
               Hours = m.Row.Sum(c => (c.TimeTo - c.TimeFrom).Hours)                 
             }

Assuming Rows is some sort of IEnumerable<T>, you can do this if you want fractions of hours too:

var item = from mainTable m 
           select new
             { 
               m.Id, 
               Hours = m.Row.Sum(c => (c.TimeTo - c.TimeFrom).TotalHours) 
             }

Or this if you want the sum of whole-number hours:

var item = from mainTable m 
           select new
             { 
               m.Id, 
               Hours = m.Row.Sum(c => (c.TimeTo - c.TimeFrom).Hours)                 
             }
乄_柒ぐ汐 2024-12-10 13:02:08

(c.TimeTo - c.TimeFrom) 的类型为 TimeSpan:

var item = from m in mainTable 
           select new { m.Id, Hours = m.Rows.Sum(c => (c.TimeTo - c.TimeFrom).TotalHours)};

(c.TimeTo - c.TimeFrom) is of type TimeSpan:

var item = from m in mainTable 
           select new { m.Id, Hours = m.Rows.Sum(c => (c.TimeTo - c.TimeFrom).TotalHours)};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文