35

我有以下业务对象:

    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);

如果我有几个类别的列表,我如何在类别列表中找到包含某个类别的项目列表?(在我的示例中,我想取回项目 2 和 3)

4

5 回答 5

113

如果你有这样的情况:

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

并且您希望获取具有类别列表中的类别的所有项目,您可以使用它:

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

Any 运算符枚举源序列,如果任何元素满足谓词给出的测试,则返回 true。在这种情况下,如果类别列表包含 ItemCategoryBO,其中 ItemCategory 字符串与项目的 ItemCategory 字符串相同,则返回 true。MSDN上有关它的更多信息

于 2012-05-24T22:19:38.263 回答
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();
于 2012-05-24T22:04:30.553 回答
1

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
于 2012-05-24T22:03:56.783 回答
1

这是我在Linqpad中所做的事情

无效的主要()
{

    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 中的 category 等于 category.ItemCategory 加入 itemInCategory
    来自 itemInCategory 中的 categoryItem
    选择新的 {item.Title, item.ItemCategory};

    itemsInCategory.Dump();
}

// 这里定义其他方法和类
      公共类 ItemCategoryBO
        {
           公共字符串 ItemCategory { 获取;放; }
           公共字符串标题 { 获取;放; }
        }

        公共类 ItemBO
        {
           公共 int ItemId { 获取;放; }
           公共字符串标题 { 获取;放; }
           公共字符串 ItemCategory { 获取;放; }
        }

这将返回:

标题、项目类别
项目1 c1
项目2 c2
项目3 c2
于 2012-05-24T22:14:08.110 回答
0

希望这可以帮助:

var result = (Object to search in).Where(m => (Object to compare to).Any(r => r.Equals(m.Key)).ToList();
于 2019-07-25T11:20:45.213 回答