按另一个列表C#过滤列表
我有以下业务对象:
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 categoryList = new List(); 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 itemList = new List(); 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);
如果我有几个类别的列表,我怎样才能找到包含类别列表中的类别的项目列表? (在我的例子中,我想回到第2和第3项)
如果你有这样的情况:
List items; List categories;
并且您希望获得具有类别列表中类别的所有项目,您可以使用:
List result = items.Where(item => categories.Any(category => category.ItemCategory.equals(item.ItemCategory)));
Any运算符枚举源序列,如果任何元素满足谓词给出的测试,则返回true。 在这种情况下,如果类别列表包含ItemCategoryBO,则其返回true,其ItemCategory字符串与项目的ItemCategory字符串相同。 有关它的更多信息,请访问MSDN
试试这个:
List items = ...; ItemCategoryBO category = ...; List filteredItems = items .Where( i => i.ItemCategory.Equals(category) ) .FirstOrDefault();
更新以解决OP的更新问题:
如果我有几个类别的列表,我怎样才能找到包含类别列表中的类别的项目列表? (在我的例子中,我想回到第2和第3项)
我认为你实际上应该分两步完成。 首先,获取您不同的项目列表。 然后,从您的商品中获取您的类别列表。 所以:
// First, get the distinct list of items List items = new List (); 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 filteredItems = items .Where( i => i.ItemCategory.Equals(category) ) .FirstOrDefault();
尝试使用一些linq
List itm = new List ; //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
这是我在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 =来自项目中的项目 在item.ItemCategory上的类别中加入类别将category.ItemCategory等于itemInCategory 来自itemInCategory中的categoryItem 选择新的{item.Title,item.ItemCategory}; itemsInCategory.Dump(); } //在此处定义其他方法和类 公共类ItemCategoryBO { public string ItemCategory {get; 组; } public string Title {get; 组; } } 公共类ItemBO { public int ItemId {get; 组; } public string Title {get; 组; } public string ItemCategory {get; 组; } }
返回:
标题,ItemCategory item1 c1 item2 c2 item3 c2