Question

I'm trying to do a C# statement in LINQPad like this:

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();

But I keep getting an error:

'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)

I pressed F4 and added every reference with "LINQ" in the name, and still no luck.

Was it helpful?

Solution

I'm trying to get the most recent TankValue record for each tank.

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
    };

OTHER TIPS

You can use them on some IQueryable objects which basically have bunch of entities, you can not directly apply them on the entity itself.

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top