locked
Getting a list of objects with max values. RRS feed

  • Question

  • User269416624 posted

    Still struggling with EF and LINQ... Embarassed

    So I have this list of objects taken from the database.  There are several properties on each object.  Let me talk about just a couple of them - Id and Distance.

    The list will have several objects with the same Id.  So you may have 6 objects with an Id of "1", 7 with an Id of "2", and so on.  The distance value within each group may be the same from some of the objects and different for others.

    What I'm trying to do is pull out a sub-list that contains just one object from each group that contains the max(Distance).  So one object with Id of "1", one object with Id of "2", and so on.  If there is more than one object within the Id group that has the same maximum number for Distance, that's OK.  Any one will do for my purposes.

    Can this be done with just one statement?  I've stumbled around trying to figure out the right combination and tripped every time.

    Wednesday, July 1, 2015 4:51 PM

Answers

  • User269416624 posted

    I ended up following a somewhat different route.  This is what I came up with:

            public List<Analysis.QdAnalyzerResult> PesEsResults {
    
                get {
    
                    List<Analysis.QdAnalyzerResult> Results = new List<Analysis.QdAnalyzerResult>();
    
                    foreach (Guid Id in this.QdResults.Select(q => q.EsId).Distinct().ToList())
                    {
    
                        int MaxDistance = this.QdResults.Where(q => q.EsId == Id).Max(q => q.QdRequiredDistance);
    
                        Results.Add(this.QdResults.Where(q => q.EsId == Id && q.QdRequiredDistance == MaxDistance).FirstOrDefault());
                        
                    }
    
                    return Results;
    
                }
    
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 3, 2015 11:01 AM

All replies

  • User1881638666 posted

    Hi,

    Try as follows,

                var max = from t in db.Tables
                    group t by t.Id
                    into grp
                    select new
                    {
                        Id = grp.Key,
                        Distance = grp.Select(item => item.Distance).Max()
                    };
    
                var resultList = max.ToList();

    Thanks,

    Wenushka

    Thursday, July 2, 2015 12:19 AM
  • User61956409 posted

    Hi dolot,

    Please refer to the following sample.

    <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("distance", typeof(int));
    
            dt.Rows.Add(1, 3);
            dt.Rows.Add(2, 4);
            dt.Rows.Add(1, 6);
            dt.Rows.Add(2, 5);
            dt.Rows.Add(3, 3);
    
            var records = from d in dt.AsEnumerable()
                            group d by d.Field<int>("Id") into newGroup
                            select new maxdistance
                            {
                                Id = newGroup.Key,
                                distance = newGroup.Select(dd => dd.Field<int>("distance")).Max()
                            };
    
            GridView1.DataSource = records;
            GridView1.DataBind();
        }
    }
    
    public class maxdistance
    {
        public int Id { get; set; }
    
        public int distance { get; set; }
    } 
    

    Best Regards,

    Fei Han



    Thursday, July 2, 2015 5:40 AM
  • User269416624 posted

    Another wrinkle that I forgot to mention.  This sublist needs to be a strongly typed object.  I'm going to use it as part of a model that will be passed to a razor view.

    Thursday, July 2, 2015 8:29 AM
  • User269416624 posted

    Hi,

    Try as follows,

                var max = from t in db.Tables
                    group t by t.Id
                    into grp
                    select new
                    {
                        Id = grp.Key,
                        Distance = grp.Select(item => item.Distance).Max()
                    };
    
                var resultList = max.ToList();

    Thanks,

    Wenushka

    So will I have to manually add all the other properties of the object to this list?

    Thursday, July 2, 2015 4:54 PM
  • User269416624 posted

    I ended up following a somewhat different route.  This is what I came up with:

            public List<Analysis.QdAnalyzerResult> PesEsResults {
    
                get {
    
                    List<Analysis.QdAnalyzerResult> Results = new List<Analysis.QdAnalyzerResult>();
    
                    foreach (Guid Id in this.QdResults.Select(q => q.EsId).Distinct().ToList())
                    {
    
                        int MaxDistance = this.QdResults.Where(q => q.EsId == Id).Max(q => q.QdRequiredDistance);
    
                        Results.Add(this.QdResults.Where(q => q.EsId == Id && q.QdRequiredDistance == MaxDistance).FirstOrDefault());
                        
                    }
    
                    return Results;
    
                }
    
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 3, 2015 11:01 AM
  • User269416624 posted

    I want to extend my thanks to both of you for taking a shot at answering my question.  Smile

    Friday, July 3, 2015 4:10 PM