Filter a List by Another List C#

How to filter a list based on another list using Linq?

Try this:

var filtered = listOfAllVenues
.Where(x=>!listOfBlockedVenues.Any(y=>y.VenueId == x.Id));

It will get all Venues where Id is not in blockedVenues list

Filter a list by another list C#

If you have a situation like:

List<ItemBO> items;
List<ItemCategoryBO> categories;

and you wish to get all the items that have a category that is in your list of categories, you can use this:

IEnumerable<ItemBO> result = items.Where(item =>
categories.Any(category => category.ItemCategory.equals(item.ItemCategory)));

The Any() operator enumerates the source sequence and returns true as soon as an item satisfies the test given by the predicate. In this case, it returns true if the categories list contains an ItemCategoryBO where its ItemCategory string is the same as the item's ItemCategory string.
More information about it on MSDN

Filter a list based on another list condition

Let's say you have a class like below:

class CustomerInformation
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

As you said, you have two lists, let's say you have two lists like below:

List<CustomerInformation> dbList = new List<CustomerInformation>
{
new CustomerInformation{Id=1, FirstName="Raju", LastName="Ahmed"},
new CustomerInformation{Id=2, FirstName="Tahira", LastName="Biswas"},
new CustomerInformation{Id=3, FirstName="Shohag", LastName="Mia"},
new CustomerInformation{Id=4, FirstName="Saiful", LastName="Islam"}
};

List<CustomerInformation> csutomerList = new List<CustomerInformation>
{
new CustomerInformation{Id=1, FirstName="Raju", LastName="Ahmed"},
new CustomerInformation{Id=2, FirstName="Tahira", LastName="Biswas"},
new CustomerInformation{Id=3, FirstName="Shohag", LastName="Mia"},
new CustomerInformation{Id=4, FirstName="Saiful", LastName="Islam"},
new CustomerInformation{Id=5, FirstName="Anny", LastName="Bishwas"},
new CustomerInformation{Id=6, FirstName="Kabita", LastName="Roy"},
new CustomerInformation{Id=7, FirstName="Zahidul", LastName="Emon"}
};

Now you want to get those DB list that are not present in the customer list with somespecific condition, so just try this:

 var newList = csutomerList.Where(cusItem => !dbList.Any(dbItem => cusItem.Id == dbItem.Id && cusItem.FirstName == dbItem.FirstName && cusItem.LastName == dbItem.LastName));

it will first find out all the data that are present in both lists and then simply deduct them.

Sample Output:

Sample Image

Full code here:

    static void Main(string[] args)
{
AvailableData();
Console.ReadKey();
}

public static void AvailableData()
{
// Create two lists.
List<CustomerInformation> dbList = new List<CustomerInformation>
{
new CustomerInformation{Id=1, FirstName="Raju", LastName="Ahmed"},
new CustomerInformation{Id=2, FirstName="Tahira", LastName="Biswas"},
new CustomerInformation{Id=3, FirstName="Shohag", LastName="Mia"},
new CustomerInformation{Id=4, FirstName="Saiful", LastName="Islam"}
};

List<CustomerInformation> csutomerList = new List<CustomerInformation>
{
new CustomerInformation{Id=1, FirstName="Raju", LastName="Ahmed"},
new CustomerInformation{Id=2, FirstName="Tahira", LastName="Biswas"},
new CustomerInformation{Id=3, FirstName="Shohag", LastName="Mia"},
new CustomerInformation{Id=4, FirstName="Saiful", LastName="Islam"},
new CustomerInformation{Id=5, FirstName="Anny", LastName="Bishwas"},
new CustomerInformation{Id=6, FirstName="Kabita", LastName="Roy"},
new CustomerInformation{Id=7, FirstName="Zahidul", LastName="Emon"}
};

var newList = csutomerList.Where(cusItem => !dbList.Any(dbItem => cusItem.Id == dbItem.Id && cusItem.FirstName == dbItem.FirstName && cusItem.LastName == dbItem.LastName));

foreach (var cust in newList)
{
Console.WriteLine("Customer Id :{0} | Customer Name: {1} | Last Name: {2} ",cust.Id,cust.FirstName,cust.LastName);
}
Console.ReadKey();

}
}

class CustomerInformation
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

Filter a list based on another list containing IEnumerableGuid using linq

You want items Where it's not true that the list Contains the id:

var filteredList = ListA.Where(x => !listOfGuids.Contains(x.id))

Filtering a List using contents of another List

What you need is to take only these items which cannot be found in the filter list. You can do it in the "old school" way, using loops:

foreach (var listItem in dataList)
{
foreach (var filterItem in filterList)
{
if (listItem == filterItem)
{
dataList.Remove(listItem);
continue;
}
}
}

Or you can use LINQ to do the filtering:

dataList.Where(d => filterList.All(f => f.Id != d.dataId))

Filter Out List with another list using LINQ

You're making a new object and then checking the list to see if that exact object/instance is in it (i.e. because it's an object, it's comparing the reference).

Instead, you need to look for overlapping IDs.

Something like this should work:

List<ServiceItem> serviceItems;
List<ServiceItemDetails> serviceItemDetails;

var result = serviceItemDetails.Where(sid => serviceItems.Any(si => si.ID == sid.ID))

In English: "The collection of ServiceItemDetails where the list of service items has an item with the same ID"

How to filter a list using another list in LINQ C#

Suppose you are having two class like below,

public class MainClass
{
public string ActualValue { get; set; }
}
public class FilterClass
{
public string Description { get; set; }
}

I am loading some dummy data like this,

        List<MainClass> mainList = new List<MainClass>();
mainList.Add(new MainClass() { ActualValue = "1000" });
mainList.Add(new MainClass() { ActualValue = "1000A" });
mainList.Add(new MainClass() { ActualValue = "1002F" });
mainList.Add(new MainClass() { ActualValue = "1002A" });
mainList.Add(new MainClass() { ActualValue = "1003" });

List<FilterClass> filterList = new List<FilterClass>();
filterList.Add(new FilterClass() { Description = "1003" });
filterList.Add(new FilterClass() { Description = "1002" });

The O/P will be given as per your requirement by,

        var output1 = mainList.Where(x => filterList.Any(y => x.ActualValue.Contains(y.Description))).ToList();

C# List Join List And Filter From Another List

We just need to first remove dropped subjects from enrolled list. then group by student.
note that we have list for each student, that it has Student and Subject as its fields, we just select the name of first student from each (as they all the same) and join the subjects together.

var result = lstEnrolled.Where(e => !lstDropped.Any(d => d.Student == e.Student && d.Subject == e.Subject))  //omit dropped courses
.GroupBy(x => x.Student) // group results by students
.Select(x => new {
Name = x.First().Student.StudentName.Split(' ').First(),
Subjects = string.Join(", ", x.Select(e => e.Subject))
}).ToArray();

for printing:

foreach(var x in result)
Console.WriteLine($"{x.Name}\t{x.Subjects}");

LIVE DEMO



Related Topics



Leave a reply



Submit