如何翻译
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
这不是一个1对1的转录,因为let
是如何工作的(你仍然可以访问let
之后的xx
),但会产生相同的结果:
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
};