Linq - group by using the elements inside an array

2020-07-18 07:58发布

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());

标签: c# linq
2条回答
孤傲高冷的网名
2楼-- · 2020-07-18 08:39

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
查看更多
▲ chillily
3楼-- · 2020-07-18 08:53

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()
             });
查看更多
登录 后发表回答