Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using Linq to filter collection with group By and select max

Posted on 2009-07-14
3
Medium Priority
?
3,082 Views
Last Modified: 2013-11-11
Given the sample code shown below, how can I use linq to filter the collection to group By field1 and select the highest value of field2, so that my new collection would have classB and classC only in it?
public class Class1
    {
        public int field1;
        public int field2;
        public DateTime field3;
        public int field4;
    }
 
    public class Class1Collection : Collection<Class1>, IEnumerable<Class1>
    {
    }
 
 
    public static class TestClass
    {
 
        public static void Test()
        {
            Class1 classA = new Class1();
            classA.field1 = 1;
            classA.field2 = 2;
            classA.field3 = new DateTime(2009, 01, 01);
            classA.field4 = 3;
 
            Class1 classB = new Class1();
            classB.field1 = 1;
            classB.field2 = 3;
            classB.field3 = new DateTime(2009, 02, 01);
            classB.field4 = 3;
 
            Class1 classC = new Class1();
            classC.field1 = 2;
            classC.field2 = 1;
            classC.field3 = new DateTime(2009, 02, 01);
            classC.field4 = 3;
 
            Class1Collection collection = new Class1Collection();
            collection.Add(classA);
            collection.Add(classB);
            collection.Add(classC);
 
            Class1Collection filteredCollection = ??
        }
 
    }

Open in new window

0
Comment
Question by:weimha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 200 total points
ID: 24851703
Try this LINQ query...
from c in collection
group c by c.field1 into grouped
from g in grouped
where g.field2 == grouped.Max( m => m.field2 )
select g;

Open in new window

0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 total points
ID: 24851753
Hi weimha;

Here is some sample code showing how this can be done.

Fernando
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication45
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        List<Class1> collection = new List<Class1>();
 
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 classA = new Class1();
            classA.field1 = 1;
            classA.field2 = 2;
            classA.field3 = new DateTime(2009, 01, 01);
            classA.field4 = 3;
 
            Class1 classB = new Class1();
            classB.field1 = 1;
            classB.field2 = 3;
            classB.field3 = new DateTime(2009, 02, 01);
            classB.field4 = 3;
 
            Class1 classC = new Class1();
            classC.field1 = 2;
            classC.field2 = 1;
            classC.field3 = new DateTime(2009, 02, 01);
            classC.field4 = 3;
 
            collection.Add(classA);
            collection.Add(classB);
            collection.Add(classC);
 
            List<Class1> filteredCollection = (from c in collection
                                               group c by c.field1 into classGroup
                                               let max = classGroup.Max(m => m.field2)
                                               from m in classGroup
                                               where m.field2 == max
                                               select m).ToList();
 
            // Display the results
            foreach (Class1 c1 in filteredCollection)
            {
                Console.WriteLine("{0,4}{1,4}{2,24}{3,4}",
                    c1.field1, c1.field2, c1.field3, c1.field4);
            }
 
        }
    }
 
    public class Class1
    {
        public int field1;
        public int field2;
        public DateTime field3;
        public int field4;
    }
}

Open in new window

0
 
LVL 15

Accepted Solution

by:
jinal earned 1600 total points
ID: 24851810

public class Class1
    {
        public int field1;
        public int field2;
        public DateTime field3;
        public int field4;
    }
 
    public class Class1Collection : Collection<Class1>, IEnumerable<Class1> , IList<Class1>
    {
    }
 
 
    public static class TestClass
    {
 
        public static void Test()
        {
            Class1 classA = new Class1();
            classA.field1 = 1;
            classA.field2 = 2;
            classA.field3 = new DateTime(2009, 01, 01);
            classA.field4 = 3;
 
            Class1 classB = new Class1();
            classB.field1 = 1;
            classB.field2 = 3;
            classB.field3 = new DateTime(2009, 02, 01);
            classB.field4 = 3;
 
            Class1 classC = new Class1();
            classC.field1 = 2;
            classC.field2 = 1;
            classC.field3 = new DateTime(2009, 02, 01);
            classC.field4 = 3;
 
            Class1Collection collection = new Class1Collection();
            collection.Add(classA);
            collection.Add(classB);
            collection.Add(classC);
 
 
            IEnumerable<Class1> data =  from cc in collection 
                        join cc1 in (
                        from c in collection
                        group c by c.field1 into g
                        select new { ID = g.Key, Max = g.Max(p => p.field2) }) on cc.field1 equals cc1.ID  where cc.field2 == cc1.Max select cc;
 
 
            Class1Collection filtercollection = new Class1Collection();
            foreach (Class1 c in data)
            {
                filtercollection.Add(c);
            }
            
            
            
 
        }
 
    }

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question