1

怎么可能翻译

var vehiclequery = db.position
    .GroupBy(c => c.device_id)
    .Select(g => g.OrderByDescending(c => c.sendtime).FirstOrDefault())
    .Select(c => new myPosition()                                                          
    {
        battery_percentage = c.battery_percentage,
        device_id = c.device_id,
        latitude = c.latitude,
        longitude = c.longitude,
        speed = c.speed,
        sendtime = c.sendtime
    });

查询语法?现在我有一些愚蠢的东西,我不知道如何让它工作。是这样的吗?

var vehiclequery = from dPosition in db.position
                   group dPosition by dPosition.device_id into xx
                   select new
                   {
                       device_id = xx.device_id
                   };

我知道缺少很多东西,但我被困在这一点上。我尝试了在堆栈上推荐的工具 - http://www.linqpad.net/,但这只能从查询语法转换为方法语法。

感谢您提供任何帮助或指导如何完成这项工作。一些有用的手册页也将不胜感激,现在我只使用http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

4

1 回答 1

4

这不是一对一的转录,因为它是如何let工作的(你仍然可以访问xxafter let),但会产生相同的结果:

var vehiclequery = from dPosition in db.position
                   group dPosition by dPosition.device_id into xx
                   let c = xx.OrderByDescending(x => x.sendtime).FirstOrDefault()
                   select new
                   {
                       battery_percentage = c.battery_percentage,
                       device_id = c.device_id,
                       latitude = c.latitude,
                       longitude = c.longitude,
                       speed = c.speed,
                       sendtime = c.sendtime
                   };

或使用子查询作为语法查询:

var vehiclequery = from dPosition in db.position
                   group dPosition by dPosition.device_id into xx
                   let c = (from x in xx
                            orderby x.sendtime desc
                            select x).FirstOrDefault()
                   select new
                   {
                       battery_percentage = c.battery_percentage,
                       device_id = c.device_id,
                       latitude = c.latitude,
                       longitude = c.longitude,
                       speed = c.speed,
                       sendtime = c.sendtime
                   };
于 2014-03-28T22:42:46.563 回答