按另一个列表过滤列表 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10745900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:04:38  来源:igfitidea点击:

Filter a list by another list C#

c#linq

提问by stillsmallvoice

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)

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

采纳答案by Diana Ionita

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

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

回答by Micah Armantrout

Try using some linq

尝试使用一些 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

回答by Charlie Kilian

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:

更新以解决 OP 的更新问题:

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)

如果我有几个类别的列表,如何在类别列表中找到包含类别的项目列表?(在我的示例中,我想取回第 2 项和第 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();

回答by dplante

Here's something I did in Linqpad

这是我在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 

回答by Fauzaan S.

Hope this helps:

希望这可以帮助:

var result = (Object to search in).Where(m => (Object to compare to).Any(r => r.Equals(m.Key)).ToList();