none
Select Distinct Values from C# List RRS feed

  • Question

  • I have the following below Linq Statement which I use to select how many Value I have in a list myList

    class MyClassDetails { public string ID; public int Value; } class MyClass { public MyClass() { moreDetails = new ObservableCollection<MyClassDetails>(); } public string Name; public ObservableCollection<MyClassDetails> moreDetails; } static void Main(string[] args) { var myList = new List<MyClass>(); var myClass1 = new MyClass(); myClass1.Name = "Class1"; myClass1.moreDetails.Add(new MyClassDetails() { Value = 1, ID="ONE" }); myClass1.moreDetails.Add(new MyClassDetails() { Value = 2, ID = "TWO" }); myClass1.moreDetails.Add(new MyClassDetails() { Value = 4, ID = "ONE" }); myClass1.moreDetails.Add(new MyClassDetails() { Value = 8, ID = "TWO" }); myClass1.moreDetails.Add(new MyClassDetails() { Value = 16, ID = "ONE" }); var myClass2 = new MyClass(); myClass2.Name = "Class2"; myClass2.moreDetails.Add(new MyClassDetails() { Value = 1, ID = "TWO" }); myClass2.moreDetails.Add(new MyClassDetails() { Value = 2, ID = "ONE" }); myClass2.moreDetails.Add(new MyClassDetails() { Value = 4, ID = "ONE" }); myClass2.moreDetails.Add(new MyClassDetails() { Value = 8, ID = "TWO" }); myClass2.moreDetails.Add(new MyClassDetails() { Value = 54, ID = "ONE" }); myList.Add(myClass1); myList.Add(myClass2); var groups2 = myList.SelectMany(myClass => myClass.moreDetails, (myClass, details) => new { details.Value, myClass.moreDetails }) .GroupBy(myClass => myClass.Value) .Select(g => new { Value = g.Key, Count = g.ToList().Distinct().Count() }); foreach (var group in groups2) { var value = group.Value;

    var count = group.Count; }

    So the above will output the following:

    group1: Value 1 Count 2

    group2: Value 2 Count 2

    group3: Value 4 Count 2

    group4: Value 8 Count 2

    group5: Value 54 Count 1

    I want to only count the Distinct Values by ID

    So for the above the groups 3 and 4 would only have count=1 as they have the same ID values

    group3: Value 4 Count 1

    group4: Value 8 Count 1

    How do I modify my query to do this?

    Sunday, November 26, 2017 3:09 PM

All replies

  • Try this:

    var groups3 = myList
                     .SelectMany( c => c.moreDetails )
                     .GroupBy( d => d.Value )
                     .Select( g => new { Value = g.Key, Count = g.GroupBy( d => d.ID ).Count() } );

    and this:

    var groups4 = myList
                     .SelectMany( c => c.moreDetails )
                     .GroupBy( d => d.Value, ( k, e ) => new { Value = k, Count = e.GroupBy( d => d.ID ).Count() } );
    
    



    • Edited by Viorel_MVP Sunday, November 26, 2017 5:00 PM
    • Proposed as answer by Fei HuModerator Monday, November 27, 2017 2:34 AM
    Sunday, November 26, 2017 4:47 PM
  • Hello LearnerMan1234,

    Try the below code. There is a way via using "groupby" statement twice. The first group by the "id" and "value". The second group by "value" based on the first result.

      var result = myList.SelectMany(MyClass => MyClass.moreDetails)
                    .GroupBy(x => new { Id = x.ID, Value = x.Value }).GroupBy(j => j.Key.Value)
                    .Select(z => new { value = z.Key, Count = z.Count() }).ToList();

    And the linq could be separated by some parts as below. This can be beneficial to debug.

     var step1 = myList.SelectMany(myClass => myClass.moreDetails, (myClass, details) => new { details.Value, details.ID }).ToList();
    
                var step2 = step1.GroupBy(x => new{ Id= x.ID, Value=x.Value});
    
                var step3 = step2.Select(g =>new {value=g.Key.Value}).ToList();
    
                var step4 = step3.GroupBy(j =>j.value);
    
                var step5=step4.Select(z=>new { value=z.Key,Count=z.Count()}).ToList();

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 28, 2017 12:17 PM
    Moderator