Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a list by another list C#

Tags:

c#

linq

I have the following business objects:

    public class ItemCategoryBO     {        public string ItemCategory { get; set; }        public string Title { get; set; }     }      public class ItemBO     {        public int ItemId { get; set; }        public string Title { get; set; }        public string ItemCategory { get; set; }      }      List<ItemCategoryBO> categoryList = new List<ItemCategoryBO>();      ItemCategoryBO itemCategory = new ItemCategoryBO();     itemCategory.ItemCategoryCd = "CARS";     itemCategory.Title = "Cars";      ItemCategoryBO itemCategory2 = new ItemCategoryBO();     itemCategory.ItemCategoryCd = "PLANES";     itemCategory.Title = "Planes";      categoryList.Add(itemCategory);     categoryList.Add(itemCategory2);      List<ItemBO> itemList = new List<ItemBO>();      ItemBO item1 = new ItemBO();     item1.ItemId = 1;     item1.Title = "1st item";     item1.ItemCategoryCd = "OTHER";      ItemBO item2 = new ItemBO();     item2.ItemId = 2;     item2.Title = "2nd Item";     item2.ItemCategoryCd = "CARS";      ItemBO item3 = new ItemBO();     item3.ItemId = 3;     item3.Title = "3rd Item";     item3.ItemCategoryCd = "PLANES";      itemList.Add(item1);     itemList.Add(item2);     itemList.Add(item3); 

If I have a list of a few categories, how could I find a list of items that contain a category in the list of categories? (In my example, I want to get back items 2 and 3)

like image 526
stillsmallvoice Avatar asked May 24 '12 21:05

stillsmallvoice


People also ask

How can I filter one list from another list in C#?

C# filter list with iteration. In the first example, we use a foreach loop to filter a list. var words = new List<string> { "sky", "rock", "forest", "new", "falcon", "jewelry" }; var filtered = new List<string>(); foreach (var word in words) { if (word. Length == 3) { filtered.

How to filter data in c#?

Filtering Data (C#) Filtering refers to the operation of restricting the result set to contain only those elements that satisfy a specified condition. It is also known as selection. The following illustration shows the results of filtering a sequence of characters.

What is Linq in C# with example?

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support.

How do I select a query in Linq?

LINQ query syntax always ends with a Select or Group clause. The Select clause is used to shape the data. You can select the whole object as it is or only some properties of it. In the above example, we selected the each resulted string elements.


2 Answers

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 if any element 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

like image 73
Diana Ionita Avatar answered Sep 17 '22 14:09

Diana Ionita


Try this:

List<ItemBO> items = ...; ItemCategoryBO category = ...;  List<ItemBO> filteredItems = items     .Where( i => i.ItemCategory.Equals(category) )     .FirstOrDefault(); 

Updated to address OP's updated question:

If I have a list of a few categories, how could I find a list of items that contain a category in the list of categories? (In my example, I want to get back items 2 and 3)

I think you actually should do this in two steps. First, get your distinct list of items. Then, from your items, get your list of categories. So:

// First, get the distinct list of items List<ItemBO> items = new List<ItemBO>(); foreach ( var category in categories ) {     foreach ( var item in category.Items )     {         if ( !items.Contains(item) )             items.Add(item);     } }  // Second, get the list of items that have the category. List<ItemBO> filteredItems = items     .Where( i => i.ItemCategory.Equals(category) )     .FirstOrDefault(); 
like image 40
Katie Kilian Avatar answered Sep 18 '22 14:09

Katie Kilian