Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq - group by using the elements inside an array property

Tags:

c#

linq

I have a number of objects and each object has an array, I would like to group these objects by the values inside the array, so conceptually they look as follows:

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

somethings that I have 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());
like image 690
Farhad-Taran Avatar asked Jul 30 '15 13:07

Farhad-Taran


2 Answers

You can use IEqualityComparer class. 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
like image 145
Hossein Narimani Rad Avatar answered Nov 09 '22 23:11

Hossein Narimani Rad


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

.GroupBy(x => string.Join("|", x.IDS))
.Select(x => new 
             {
                  IDS = x.Key.Split('|').Where(s => s != string.Empty).ToArray(),
                  Count = x.Count()
             });
like image 42
Simon_Weaver Avatar answered Nov 09 '22 23:11

Simon_Weaver