无法将匿名返回类型从查询转换为实体类型

发布于 2024-09-10 04:16:00 字数 569 浏览 3 评论 0原文

我正在努力解决一个(通常很容易处理)问题。

我有一个包含“测量”表的数据库。每个测量都有一个时间戳(日期时间)、一个值(十进制)、一个测量类型(查找表的外键)并且属于一个“容量项”(主表)。

我在实体框架中导入了 SQL 数据库并用它创建了一个模型。现在我想查询基线。基线是容量项的当前状态,并且由该特定容量项的每种类型的最新测量值来表示。

所以查询需要返回的是:对于某个CapacityItem,给我每个测量类型的最新测量值。在 SQL 查询中,我会对测量类型执行“分组依据”,并对时间戳执行 MAX()。 但在 Linq-2-Entities 中我似乎在泥沼中挣扎。我需要一个返回 IQueryable 的函数,其中包含所有基线测量值,但我的查询都返回某种无法进行类型转换的匿名类型。

我希望我说清楚了。当我重新阅读这篇文章时,我可以想象它没有多大意义。但我已经看这个太久了,我的脑子开始做有趣的事情:-)

这里有人可以让我朝正确的方向前进吗?如果需要,请要求澄清。

预先感谢一百万。

〜罗布

I'm struggling with a (usually simple to deal with) problem.

I have a database containing a "measurements" table. Each measurement has a timestamp (datetime), a value (decimal), a measurement type (foreign key to a lookup table) and belongs to a "capacity item" (a master table).

I imported the SQL database in Entity Framework and created a model out of it. Now I want to query for the base line. A base line is the current status of the capacity item, and is represented by the latest measurements for each type of that particular capacity item.

So what the query needs to return is: for a certain CapacityItem, give me the latest measurement of each measurement type. In an SQL query I would do a "group by" on the measurement type and do a MAX() on the timestamp.
But in Linq-2-Entities I seem to be swinning in the mud. I need a function that returns IQueryable<Measurement>, containing all baseline measurements, but my queries all return some sort of anonymous type that's impossible to typecast.

I hope I made myself clear. When I re-read this, I can image it doesn't make much sense. But I have been looking at this far too long, and my mind starts doing funny things :-)

Anyone here that can get me in the right direction? Please ask for clarification if needed.

Thanks a million in advance.

~Rob

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

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

发布评论

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

评论(1

月棠 2024-09-17 04:16:00

类似这样的:

var q = from m in Context.Measurements
        group m by m.MeasurementType.Id into group
        from bl in group
        where bl.TimeStamp == group.Max(g => g.TimeStamp)
        select bl;

这是我的想法并猜测你的数据库。你可能需要调整它。

Something like:

var q = from m in Context.Measurements
        group m by m.MeasurementType.Id into group
        from bl in group
        where bl.TimeStamp == group.Max(g => g.TimeStamp)
        select bl;

This is off the top of my head and guessing about your DB. You may have to tweak it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文