1

This is from LinqPad (hence the .Dump())

void Main()
{
    var k1 = new List<int>(){1,2,3,4,5};
    var k2 = new List<int>(){1,2,3,4,5};

    var o1 = new A(){k=k1, objectName="o1"};
    var o2 = new A(){k=k2, objectName="o2"};

    var l = new List<A>();

    l.Add(o1);
    l.Add(o2);

    var b = from a in l 
    group a.objectName by a.k into g
    select new {g.Key, n = String.Join(",",g)};

    b.Dump();
}

public class A {
    public List<int> k;
    public string objectName;
}

The problem is that it doesn't work, the code above yields:

enter image description here

I know why this is happening, it's because the list objects are separate objects, but what I was wondering is if there is a way to tell group by to use content of the list rather than the object instance.

Matt
  • 25,943
  • 66
  • 198
  • 303
  • 2
    You can use `GroupBy` method that takes a custom comparer, and check your lists for equality that way. – Sergey Kalinichenko Jul 31 '15 at 02:14
  • Here is article explaining the custom comparer: http://blogs.msdn.com/b/csharpfaq/archive/2009/03/25/how-to-use-linq-methods-to-compare-objects-of-custom-types.aspx (applies to all LINQ methods that use equality - GroupBy is similar to Distinct used in the article from comparison point of view). I can't find good exact duplicate (I'm quite sure there are plenty), but here is one close enough question to start search if you would like to close as duplicate - http://stackoverflow.com/questions/1365748/distinct-not-working-with-linq-to-objects?rq=1 – Alexei Levenkov Jul 31 '15 at 02:41

3 Answers3

2
public static void Main()
{
    var k1 = new List<int>(){1,2,3,4,5};
    var k2 = new List<int>(){1,2,3,4,5};

    var o1 = new A(){k=k1, objectName="o1"};
    var o2 = new A(){k=k2, objectName="o2"};

    var l = new List<A>();

    l.Add(o1);
    l.Add(o2);

    // Use custom comparer for the list
    var b = l.GroupBy(a => a.k, new ListComparer<int>())
             .Select(g => new 
                {
                    Key = String.Join(",", g.Key.Select(i => i)), 
                    n = String.Join(",",g.Select(i => i.objectName))
                });


    foreach(var item in b)
    {
        Console.WriteLine(string.Format("{0} : {1}", item.Key, item.n));
        // 1,2,3,4,5 : o1,o2
    }
}

public class A
{
    public List<int> k;
    public string objectName;
}


public class ListComparer<T> : IEqualityComparer<List<T>>
{
    // Ignore the order and compare with the sequence value for the equality
    public bool Equals(List<T> left, List<T> right)
    {
        return left.OrderBy(i => i).SequenceEqual(right.OrderBy(i => i));
    }

    public int GetHashCode(List<T> list)
    {
        return list.Count;
    }
}

.Net Fiddle

Eric
  • 5,675
  • 16
  • 24
2

You can convert array to string.

var b = l.GroupBy(o => string.Join(";", o.k))
         .Select(g => new { Key = g.First().k,
                            n = string.Join(",", g.Select(o => o.objectName)) });
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31
0

I managed to accomplish the following:

var b = l.SelectMany(a => a.k.Select(i =>  new { Key = i, n = a.objectName }))
         .GroupBy(i => i.Key);

LINQPad Image

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154