开发者

Select Max value from sublist with LINQ

How to write a LINQ query that would return row of type "Value" with Max Date and Max value for that date. it should be the row where name = "Correct"

I've written a query at the end, and it working, just trying to find a correct way of doing this.

Thanks in advance

public class MeasurePoint
{
    public int No { get; set; }
    public string Weight { get; set; }
    public List<Values> Vals { get; set; }
}

public class Values
{
    public string Name { get; set; }
    public int Val { get; set; }
    public DateTime Date { get; set; }
}

public static class Test
{
    public static void Calc()
    {
        var mps = new List<MeasurePoint>();

        mps.Add(new MeasurePoint()
        {
            No = 1,
            Vals = new List<Values>()
            {
                new Values(){Date = DateTime.Now.Date.AddDays(1), Name = "testas", Val = 1},
                new Values(){Date = DateTime.Now.Date.AddDays(2), Name = "testas", Val = 5},
                new Values(){Date = DateTime.Now.Date, Name = "testas", Val = 15}
            }});
        mps.Add(new MeasurePoint()
        {
            No = 2,
            Vals = new List<Values>()
            {
                new Values(){Date = DateTime.Now.Date, Name = "testas", Val = 11},
                new Values(){Date = DateTime.Now.Date.AddDays(2), Name = "Correct", Val = 55},
                new Values(){Date = DateTime.Now.Date, Name = "testas", Val = 15}
            }
        });
        mps.Add(new MeasurePoint()
        {
            No = 3,
            Vals = new List<Values>()
            {
                new Values(){Date = DateTime.Now.Date.AddDays(1), Name = "testas", Val = 111},
                new Values(){Date = DateTime.Now.Date.AddDays(2), Name = "testa开发者_运维百科s", Val = 52},
                new Values(){Date = DateTime.Now.Date, Name = "testas", Val = 15}
            }
        });

        mps.Add(new MeasurePoint()
        {
            No = 4,
            Vals = new List<Values>()                      
        });

        var x = mps.ElementAt(0).Vals.Union(mps.ElementAt(1).Vals).Union(mps.ElementAt(2).Vals);
        var z = x.Where(p => p.Date == x.Max(d => d.Date)).MaxBy(t=>t.Val);

        //One more way I've found
        var ttt = mps.SelectMany(p => p.Vals).GroupBy(t=>t.Date).MaxBy(r=>r.Key).MaxBy(g=>g.Val);
    }


var max = mps.SelectMany(x => x.Vals)
    .Aggregate((a, x) => (x.Date > a.Date) ||
                         ((x.Date == a.Date) && (x.Val > a.Val)) ? x : a);


try this

var result = mps.Where(m => m.Vals.Count > 0)
    .SelectMany(m => m.Vals
        .OrderByDescending(v => v.Date)
        .Take(1), (m, v) => new {m.No, v.Date, v.Name, v.Val});

EDIT - this is a new version as issue has become more clear

var result = mps.Where(m => m.Vals.Count > 0)
    .SelectMany(m => m.Vals)
    .OrderByDescending(v => v.Date)
    .ThenByDescending(v => v.Val).Take(1);


How can use call MaxBy() if it's a method of IObservable<T> meanwhile GroupBy() returns IEnumerable<T> ?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜