【问题标题】:Filter a list by another list C#按另一个列表过滤列表 C#
【发布时间】:2012-05-24 21:59: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 项)

【问题讨论】:

  • 你的清单是什么?可以肯定的是,这里的“过滤依据”是什么意思?
  • 我真的不明白这里的反对票。弄清楚 OP 的要求并不难。如果你真的认为它已经足够糟糕以至于需要投反对票,请给 OP 留下一些反馈来解释你投反对票的原因。
  • @CharlieKilian 真的不明白吗?评论后It isn't very hard to figure out what the OP is asking for?
  • 抱歉没有详细说明,我已经更新了我的问题 :) 谢谢。

标签: c# linq


【解决方案1】:

如果你有这样的情况:

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

【讨论】:

  • 非常感谢戴安娜,对 linq 还是有点陌生​​。正是我想要的!
  • 也为我工作
  • 此代码的潜在修订:List&lt;ItemBO&gt; result = items.Where(item =&gt; categories.Any(category =&gt; category.ItemCategory.equals(item.ItemCategory))).ToList(); 如目前所写,您将收到错误,因为它返回 IEnumerable 而不是 List
  • 优秀。但返回将是 IEnumerable
  • 2 天我一直在为在 LINQ 中的 SQL 中做我能做的事情而奋斗,我终于可以用这个来做。这样的“啊哈”时刻
【解决方案2】:

试试这个:

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

【讨论】:

  • 如何为类别列表执行此操作?
  • @CharlieKilian 我会尽快取消我的反对票,我明白你的答案是什么
【解决方案3】:

尝试使用一些 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,我更新了我的问题,我正在寻找一种基于另一个列表动态过滤的方法,而不仅仅是一个项目的类别
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    希望这会有所帮助:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多