开发者

Cannot cast anonymous return type from query to an entity type

开发者 https://www.devze.com 2023-01-07 17:46 出处:网络
I\'m struggl开发者_JAVA技巧ing 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 m

I'm struggl开发者_JAVA技巧ing 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号