Filter a list by another list C#
Solution 1:
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
Solution 2:
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();
Solution 3:
Try using some linq
List<ItemBO> itm = new List<ItemBO>;
//Fill itm with data
//get selected item from control
string selectedcategory = cboCatetories.SelectedItem;
var itms = from BO in itm where itm.ItemCategory = selectedcategory select itm;
itms now contains all items in that category
Solution 4:
Here's something I did in Linqpad
void Main() { var cat1 = new ItemCategoryBO {ItemCategory="c1", Title = "c1"}; var cat2 = new ItemCategoryBO {ItemCategory="c2", Title = "c2"}; var item1 = new ItemBO { ItemId = 1, Title = "item1", ItemCategory="c1"}; var item2 = new ItemBO { ItemId = 1, Title = "item2", ItemCategory="c2"}; var item3 = new ItemBO { ItemId = 1, Title = "item3", ItemCategory="c2"}; var item4 = new ItemBO { ItemId = 1, Title = "item4", ItemCategory="c3"}; var items = new List() {item1, item2, item3, item4}; var categories = new List() {cat1, cat2}; var itemsInCategory = from item in items join category in categories on item.ItemCategory equals category.ItemCategory into itemInCategory from categoryItem in itemInCategory select new {item.Title, item.ItemCategory}; itemsInCategory.Dump(); } // Define other methods and classes here 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; } }
This returns:
Title, ItemCategory item1 c1 item2 c2 item3 c2