Linq - group by using the elements inside an array property

Farhad-Taran

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());
Hossein Narimani Rad

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Linq - group by using the elements inside an array property

From Dev

LINQ group by query using reflected property name

From Dev

Group by with select new array using linq

From Dev

All elements of String() Array ToLower, using LINQ

From Dev

Linq group by property performance

From Dev

Randomly group elements of array using Javascript

From Dev

Randomly group elements of array using Javascript

From Dev

Group objects in List by property in object that is List using LINQ

From Dev

How to group a Data list inside a another list using linq?

From Dev

c# - index of array entries inside another array using LINQ

From Dev

LINQ Splitting and group by resulting property

From Dev

Linq Where/Group By on XML Elements

From Dev

Group records stored in one-dimensional array using LINQ

From Dev

Convert delimited string to array and group using LINQ in C#

From Dev

Group objects in array by property

From Dev

Linq - Group then compare elements within each group

From Dev

How to add an object property inside an array using a variable name for javascript?

From Dev

AngularJS - Update nested object property inside array using $http

From Dev

group by in mongodb on array elements

From Dev

group elements in an array and sum

From Dev

arranging array elements in a group

From Dev

Exploitation of data provider elements inside test as array, using testNG

From Dev

How to search for a string inside an array elements using javascript?

From Dev

map array to elements text using jQuery.each inside a for loop?

From Dev

Using LINQ for Group and Sum

From Dev

group by and count using linq

From Dev

Group by Distinct using Linq

From Dev

Using LINQ to group numbers

From Dev

Linq to SQL using Group by

Related Related

  1. 1

    Linq - group by using the elements inside an array property

  2. 2

    LINQ group by query using reflected property name

  3. 3

    Group by with select new array using linq

  4. 4

    All elements of String() Array ToLower, using LINQ

  5. 5

    Linq group by property performance

  6. 6

    Randomly group elements of array using Javascript

  7. 7

    Randomly group elements of array using Javascript

  8. 8

    Group objects in List by property in object that is List using LINQ

  9. 9

    How to group a Data list inside a another list using linq?

  10. 10

    c# - index of array entries inside another array using LINQ

  11. 11

    LINQ Splitting and group by resulting property

  12. 12

    Linq Where/Group By on XML Elements

  13. 13

    Group records stored in one-dimensional array using LINQ

  14. 14

    Convert delimited string to array and group using LINQ in C#

  15. 15

    Group objects in array by property

  16. 16

    Linq - Group then compare elements within each group

  17. 17

    How to add an object property inside an array using a variable name for javascript?

  18. 18

    AngularJS - Update nested object property inside array using $http

  19. 19

    group by in mongodb on array elements

  20. 20

    group elements in an array and sum

  21. 21

    arranging array elements in a group

  22. 22

    Exploitation of data provider elements inside test as array, using testNG

  23. 23

    How to search for a string inside an array elements using javascript?

  24. 24

    map array to elements text using jQuery.each inside a for loop?

  25. 25

    Using LINQ for Group and Sum

  26. 26

    group by and count using linq

  27. 27

    Group by Distinct using Linq

  28. 28

    Using LINQ to group numbers

  29. 29

    Linq to SQL using Group by

HotTag

Archive