Tuesday, October 11, 2011

Compare Collections in C#

Often we need to check if the items in the collections are same or not. It comes into action very often during unit testing.

Here we will see three ways of doing so

Entity Class (Player Entity)

namespace ConsoleApplication1
{
    public class Players
    {
        public int PlayerId { get; set; }    
        public string PlayerName { get; set; }
        public string BelongsTo { get; set; }
        public int PlayerAge { get; set; }
        public int FeePerMatch { get; set; }
    }
}

And let the have our source ready as under

private static List GetPlayerList1()
        {
            List lstPlayers = new List();
            Enumerable
                .Range(1, 10)
                .ToList()
                .ForEach(i => lstPlayers.Add(new Players
                {
                    PlayerId = i
                    ,
                    PlayerName = string.Concat("PlayerName", i)
                    ,
                    BelongsTo = i % 2 == 0 ? "India" : "USA"
                    ,
                    PlayerAge = i + 20
                    ,
                    FeePerMatch = i + 1000
                }));
            return lstPlayers;
        }

        private static List GetPlayerList2()
        {
            List lstPlayers = new List();
            Enumerable
                .Range(1, 10)
                .ToList()
                .ForEach(i => lstPlayers.Add(new Players
                {
                    PlayerId = i
                    ,
                    PlayerName = string.Concat("PlayerName", i)
                    ,
                    BelongsTo = i % 2 == 0 ? "India" : "USA"
                    ,
                    PlayerAge = i + 20
                    ,
                    FeePerMatch = i + 1000
                }));
            return lstPlayers;
        }
    }

Approach 1 : Using Union extension method

var source1 = GetPlayerList1();
var source2 = GetPlayerList2();

var result = source1.Where(x1 => !source2.Any(x2 => x1.PlayerId == x2.PlayerId
                                                              && x1.PlayerName == x2.PlayerName
                                                              && x1.BelongsTo == x2.BelongsTo
                                                              && x1.PlayerAge == x2.PlayerAge
                                                              && x1.FeePerMatch == x2.FeePerMatch))
                         .Union(
                          source2.Where(x1 => !source1.Any(x2 => x1.PlayerId == x2.PlayerId
                                                              && x1.PlayerName == x2.PlayerName
                                                              && x1.BelongsTo == x2.BelongsTo
                                                              && x1.PlayerAge == x2.PlayerAge
                                                              && x1.FeePerMatch == x2.FeePerMatch)));


            if (result.Count() > 0) Console.WriteLine("Objects are not equal");
            else Console.WriteLine("Objects are equal");

            Console.ReadKey(true);
//Output
//Objects are equal


Read more: Beyond Relational
QR: compare-collections-in-c.aspx

Posted via email from Jasper-Net