C# 如何在LINQPad中使用Max()、First()和OrderBy()等LINQ方法?

C# 如何在LINQPad中使用Max()、First()和OrderBy()等LINQ方法?,c#,linq,linqpad,C#,Linq,Linqpad,我试图在LINQPad中做一个C#语句,如下所示: var result = from tv in TankValues join t in Tanks on tv.TankID equals t.ID orderby t.Description, tv.LogDate descending select new { Description = t.Description, LogDate = tv.Max(l =>

我试图在LINQPad中做一个C#语句,如下所示:

var result = 
    from tv in TankValues 
    join t in Tanks on tv.TankID equals t.ID
    orderby t.Description, tv.LogDate descending  
    select new {
        Description = t.Description,
        LogDate = tv.Max(l => l.LogDate),
        Level = tv.LevelPercentTotal
    };

result.Dump();
但我不断地得到一个错误:

'LINQPad.User.TankValues' does not contain a definition for 'Max' and no 
extension method 'Max' accepting a first argument of type 
'LINQPad.User.TankValues' could be found (press F4 to add a using 
directive or assembly reference)

我按下F4键,在名称中添加了每个带有“LINQ”的引用,但仍然没有运气

您可以在一些基本上有一堆实体的
IQueryable
对象上使用它们,但不能直接将它们应用于实体本身

var result = 
    (from tv in TankValues 
    join t in Tanks on tv.TankID equals t.ID
    orderby t.Description, tv.LogDate descending  
    select new {
        Description = t.Description,
        LogDate = tv.MaxDate,// or TankValues.Max(x=>x.LogDate) // if you need max date here
        Level = tv.LevelPercentTotal
    }).Max(l => l.LogDate)//This is just to show that Max works here;

result.Dump();
我正在尝试获取每个油箱的最新油箱值记录


恐怕信息是正确的。不能对实体调用
Max
方法。对于一个
TankValue
,总是只有一个
LogDate
,不是吗?您可能需要
分组依据
子句。啊。。因此,它与查询语句有关,而不是与LinqPad限制有关。恐怕情况就是这样。您想用
Max
做什么?你不是只想把
tv
分配给
LogDate
?@MarcinJuraszek击中了它的头部。我正在尝试获取每个油箱的最新油箱值记录。组员被要求做我想做的事情。除了
t.Key.Description
应该是
g.Key.Description
之外,其他都有效。
var result = 
    from t in Tanks
    join tv in TankValues on t.ID equals tv.TankID
    group tv by new { t.ID, t.Description } into g
    orderby g.Key.Description descending  
    select new {
        Description = g.Key.Description,
        LogDate = g.OrderByDescending(x => x.LogDate).FirstOrDefault(),
        Level = g.OrderByDescending(x => x.LogDate).FirstOrDefault().LevelPercentTotal
    };