Linq - group using elements inside array property

I have multiple objects and each object has an array, I would like to group these objects by values ​​within the array, so conceptually they look like this:

var objects = new []{
  object1 = new object{
    elements = []{1,2,3}
  },
  object2 = new object{
    elements = []{1,2}
  },
  object3 = new object{
    elements = []{1,2}
  },
  object4 = new object{
    elements = null
  }
}

      

after grouping:

group1: object1
group2: object2,object3
group3: object4

      

something i tried: actual classes:

    public class RuleCms
        {
            public IList<int> ParkingEntitlementTypeIds { get; set; }
        }


var rules = new List<RuleCms>()
        {
            new RuleCms()
            {
                ParkingEntitlementTypeIds = new []{1,2}
            },
            new RuleCms()
            {
                ParkingEntitlementTypeIds = new []{1,2}
            },
            new RuleCms()
            {
                ParkingEntitlementTypeIds = new []{1}
            },
            new RuleCms()
            {
                ParkingEntitlementTypeIds = null
            }
        };

var firstTry = rules.GroupBy(g => new { entitlementIds = g.ParkingEntitlementTypeIds, rules = g })
                    .Where(x => x.Key.entitlementIds !=null && x.Key.entitlementIds.Equals(x.Key.rules.ParkingEntitlementTypeIds));

var secondTry =
            rules.GroupBy(g => new { entitlementIds = g.ParkingEntitlementTypeIds ?? new List<int>(), rules = g })
                .GroupBy(x => !x.Key.entitlementIds.Except(x.Key.rules.ParkingEntitlementTypeIds ?? new List<int>()).Any());

      

+3


source to share


2 answers


You can use the class IEqualityComparer

. Here is the code:

class MyClass
{
    public string Name { get; set; }
    public int[] Array { get; set; }
}

class ArrayComparer : IEqualityComparer<int[]>
{
    public bool Equals(int[] x, int[] y)
    {
        return x.SequenceEqual(y);
    }

    public int GetHashCode(int[] obj)
    {
        return string.Join(",", obj).GetHashCode();
    }
}

      



Then

var temp = new MyClass[]
{
    new MyClass { Name = "object1", Array = new int[] { 1, 2, 3 } },
    new MyClass { Name = "object2", Array = new int[] { 1, 2 } },
    new MyClass { Name = "object3", Array = new int[] { 1, 2 } },
    new MyClass { Name = "object4", Array =null }
};

var result = temp.GroupBy(i => i.Array, new ArrayComparer()).ToList();
//Now you have 3 groups

      

+4


source


For simple data that is really as simple as your example, you can do this:



.GroupBy(x => string.Join("|", x.IDS))
.Select(x => new 
             {
                  IDS = x.Key.Split('|').Where(s => s != string.Empty).ToArray(),
                  Count = x.Count()
             });

      

0


source







All Articles