-1

エネルギー消費値の履歴を示す SQL テーブルがあります。ここで、時間、日、週、月、年ごとに統計を取得するために、いくつかのクエリを実行する必要があります。もちろん、それぞれ別々です。クエリの例は次のとおりです。

from c in context.ConsumptionSet
join e in context.EquipmentSet on c.EquipmentID equals e.EquipmentID
orderBy c.Date
group new { c, e } by c.Date into grp
 select new
{
    date = grp.Key.Day,
    value = grp.GroupBy(i => i.e.EquipmentID).Sum(g => g.Max(i => i.c.Value))
})

ご覧のとおり、リストを最初に日付 (DateTime 型) で並べ替えてから、日ごとにグループ化する必要があります。ただし、この上記のクエリでは、例外的な結果が得られませんでした。私はそこに何が欠けていますか?

4

1 に答える 1

0

Assuming that you are storing values with date and time, then grouping by them will have little or no effect. To group, you need equal values to group on.

For example:

Id |  Date
---+------------------------
1  |  2013-01-01 1:23:45.333
2  |  2013-01-01 2:34:56.667

Grouping by Date will not help, because the values are different. Instead, I'd need to group by something more meaningful, perhaps by day.

To do this in Entity Framework, use the methods in the EntityFunctions or SqlFunctions classes. For example:

from foo in context.Whatever
group foo by EntityFunctions.TruncateTime(foo.Date) into grp
...

Now you are grouping by just the date. Since both rows are on 2013-01-01, they will be grouped together.

于 2013-08-01T16:36:43.010 に答える