用另一个列表筛选列表 C#

39

我有以下业务对象:

    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)


你的列表是什么?并且确切地说,“按...筛选”在这里是什么意思? - H H
1
我真的不明白这里为什么会有负评。弄清楚 OP 在问什么并不是很难。如果你真的认为它糟糕到值得一个负评,那就给 OP 一些反馈,解释一下为什么你给他们点了个踩。 - Katie Kilian
@CharlieKilian真的不明白吗?在评论“弄清楚OP在问什么并不是很难”的之后? - L.B
抱歉之前没有提供足够的细节,我已经更新了我的问题 :) 谢谢。 - stillsmallvoice
5个回答

118

如果你遇到这样的情况:

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

非常感谢你,Diana。我对Linq还有点陌生。这正是我在寻找的! - stillsmallvoice
我也试过了,有效。 - Rush.2707
3
目前这段代码可能需要进行修改:List<ItemBO> result = items.Where(item => categories.Any(category => category.ItemCategory.equals(item.ItemCategory))).ToList();目前的写法会返回一个 IEnumerable 类型,而不是 List<T> 类型,所以可能需要进行修正。 - cbattlegear
很好。但返回值将是IEnumerable<ItemBO>。 - Abu Bakar Siddik
前两天我在尝试使用LINQ来完成SQL中的操作,经过不断努力,终于用这个方法成功了。真是一种恍然大悟的时刻。 - taimur alam

1

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

这将返回:

标题,项目类别
项目1 c1
项目2 c2
项目3 c2

1

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

谢谢Micah,我更新了我的问题,我正在寻找一种根据另一个列表动态筛选的方法,而不仅仅是一个项目的类别。 - stillsmallvoice

1

试试这个:

List<ItemBO> items = ...;
ItemCategoryBO category = ...;

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

更新以回答 OP 的问题更新:

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

我认为你实际上应该分两步来完成。首先,获取您的不同项目列表。然后,从您的项目中获取您的类别列表。所以:

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

我如何为类别列表做到这一点? - stillsmallvoice
@CharlieKilian 我会立即取消我的踩票,我理解你的回答所回答的问题。 - L.B

0
希望这能有所帮助:
var result = (Object to search in).Where(m => (Object to compare to).Any(r => r.Equals(m.Key)).ToList();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接