How to compare two lists in C#.

There are many reasons why you would need to compare two lists in a web application.

In this blog, we have a company that has sales reps, and they can only see customers who are in the region that the sales rep covers. To achieve this, we need to compare the sales reps region with the customer's area and if they match, then display that customer to the sales rep.

To achieve this, we need to create two view models, one for the sales rep and one for the customers. In a live application, the data would be taken from a database, but for this example, I am just hard coding the values.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DemoSite.Models
    public class CustomerViewModel
        public CustomerViewModel(string regionCode, string region, string customer, string phoneNumber, bool hasFollowedUp)
            RegionCode      = regionCode;
            Region          = region;
            Customer        = customer;
            PhoneNumber     = phoneNumber;
            HasFollowedUp   = hasFollowedUp;
        public string RegionCode        { get; }
        public string Region            { get; }
        public string Customer          { get; }
        public string PhoneNumber       { get; }
        public bool HasFollowedUp       { get; }

    public class SalesRepViewModel
        public SalesRepViewModel(string regionCode)
            RegionCode = regionCode;
        public string RegionCode { get; }


Now that we have the view models created let's write the hardcoded values in the controller, and then we can run the code and see what results we get.

So in the controller, we have the following code.

public ActionResult Index()
            List customerViewModels = new List
                new CustomerViewModel("N", "North","Customer 1","123456789",false),
                new CustomerViewModel("ne", "North East","Customer 2","1456331",false),
                new CustomerViewModel("Nw", "North West","Customer 3","78552136",false),
                new CustomerViewModel("s", "South","Customer 4","47845369",false),
                new CustomerViewModel("SE", "South East","Customer 5","143256789",false),
                new CustomerViewModel("SW", "South West","Customer 6","7845236",false),

            List salesRepViewModel = new List
                new SalesRepViewModel("NE"), 
                new SalesRepViewModel("NW")

            customerViewModels.RemoveAll(u => salesRepViewModel.Count(f => u.RegionCode.ToLower().Contains(f.RegionCode.ToLower())) == 0);

            var data = customerViewModels.ToList();

            return View();

Notice in the 'CustomerViewModel' the region code is a mixture of upper and lower case text. This could be an issue, so to solve it when making the comparison we make all text lowercase.

The line of code that does all the work is: customerViewModels.RemoveAll(u => salesRepViewModel.Count(f => u.RegionCode.ToLower().Contains(f.RegionCode.ToLower())) == 0);

What the line of code above is doing is comparing the salesRepViewModel against the customerViewModel and removing all the region codes that have no match.

The results can be seen in the image below.

How to compare two lists in C#

How to compare two lists in C#

The example above shows how to compare two lists in a website application, once done, you would then display the results on the frontend and do whatever needs to be done.

I hope you found this short example helpful.