1

我在我的一个项目中使用 BLToolKit,我试图让它工作。我不喜欢的是我试图将一堆临时时间平均到分钟,但是正在生成的 select 语句按分钟分组,然后选择原始时间。我认为我正确地执行了 linq 表达式(但话又说回来,我没有得到我期望的结果)。(这是 C#,如果你在乎的话)有人知道出了什么问题吗?

                 var test = (from r in db.SensorReadingRaws
                        where r.TimeLogged < DateTime.Now.AddMinutes(-2)
                        group r by new
                        {
                            Sensor = r.SensorNumber,
                            //group time down to the minute
                            Time = r.TimeLogged.AddSeconds(-1 * r.TimeLogged.Second).AddMilliseconds(-1 * r.TimeLogged.Millisecond)
                        } into grouped
                        select new SensorReading
                        {
                            SensorNumber = grouped.Key.Sensor,
                            TimeLogged = grouped.Key.Time,
                            Reading = (int)grouped.Average(x => x.Reading)
                        }).ToList();

            textBox1.Text = db.LastQuery;

结果查询是这样的

SELECT
[r].[SensorNumber],
[r].[TimeLogged],
Avg([r].[Reading]) as [c1]
FROM
[SensorReadingRaw] [r]
WHERE
[r].[TimeLogged] < @p1
GROUP BY
[r].[SensorNumber],
DateAdd(Millisecond, Convert(Float, -DatePart(Millisecond, [r].[TimeLogged])), DateAdd(Second, Convert(Float, -DatePart(Second, [r].[TimeLogged])), [r].[TimeLogged])),
[r].[TimeLogged]
4

2 回答 2

6

我发现

BLToolkit.Data.Linq.Sql.AsSql<T>(T obj)

可以用作这种情况的解决方法。

在选择语句中将此功能应用于所需的分组键属性时,您可以摆脱分组/选择原始字段。

它可能看起来像:

_queryStore.Leads().
    GroupBy(x => new {
        x.LeadDate.Hour,
        x.LeadDate.Minute
    }).
    Select(x => new {
        Hour = Sql.AsSql(x.Key.Hour),
        Minute = Sql.AsSql(x.Key.Minute),
        Count = x.Count()
    });

在您的特定情况下:

                 var test = (from r in db.SensorReadingRaws
                        where r.TimeLogged < DateTime.Now.AddMinutes(-2)
                        group r by new
                        {
                            Sensor = r.SensorNumber,
                            //group time down to the minute
                            Time = r.TimeLogged.AddSeconds(-1 * r.TimeLogged.Second).AddMilliseconds(-1 * r.TimeLogged.Millisecond)
                        } into grouped
                        select new SensorReading
                        {
                            SensorNumber = grouped.Key.Sensor,
                            TimeLogged = Sql.AsSql(grouped.Key.Time),
                            Reading = (int)grouped.Average(x => x.Reading)
                        }).ToList();
于 2013-08-28T12:04:02.757 回答
2

我昨天遇到了同样的问题。今天我找到了一个解决方法。这个想法是编写 2 个linq查询。首先转换数据,然后对结果进行分组:

var bandAndDate =
    (from r in repo.Entities
    select new {Band = r.Score / 33, r.StartTime.Date});

var examsByBandAndDay =
    (from r in bandAndDate
    group r by new {r.Band, r.Date } into g
    select new { g.Key.Date, g.Key.Band, Count = g.Count() }).ToList();

这两个查询都运行一个 SQL 来完成这项工作:

SELECT
    [t1].[c1] as [c11],
    [t1].[c2] as [c21],
    Count(*) as [c3]
FROM
(
    SELECT
        [r].[Score] / 33 as [c2],
        Cast(Floor(Cast([r].[StartTime] as Float)) as DateTime) as [c1]
    FROM
        [Results] [r]
) [t1]
GROUP BY
    [t1].[c2],
    [t1].[c1]
于 2012-05-02T07:06:11.803 回答