【问题标题】:Get all the Items from Generic List by Specific value按特定值从通用列表中获取所有项目
【发布时间】:2019-12-18 23:58:54
【问题描述】:

我有一个通用列表,其中有很多项目。有些项目具有特定值,例如 true,这里我需要运行一个 linq 查询,以使基于 items value=true 的特定项目能够在 var 变量中的这些项目中分离。

我尝试过使用 List.Find、FindAll、Contains

public class FilterControl  
{  
    public bool IsItem1 { get; set; }  
    public bool IsItem2 { get; set; }  
    public bool IsItem3 { get; set; }  
    public bool IsItem4 { get; set; }  
    public bool IsItem5 { get; set; }  
    public bool IsItem6 { get; set; }  
    public bool IsItem7 { get; set; }  
    public bool IsItem8 { get; set; }  
    public bool IsItem9 { get; set; }  
    public bool IsItem10 { get; set; }  
}  

List<FilterControl> listFilters = new List<FilterControl>(){  
   new FilterControl() { IsItem1 = false, IsItem2 = false, IsItem3 = true, IsItem4 = false, IsItem5 = true, IsItem6 = true, IsItem7 = false, IsItem8 = true, IsItem9 = false, IsItem2 = true },  
}; 

我试过如下:

var getAllItemsWchValueTrue = listFilters.Where(a => a.IsItem1 == true).Select(a => a.IsItem1).FirstOrDefault();

但在这段代码中,我只能检查一个项目的值为真或假。 我需要在这里检查那些我想在 Var 变量中拥有的项目的值是否为真。

【问题讨论】:

  • 您发布的代码只是创建了列表。您尝试了哪些 LINQ 查询,发生了什么?为什么项目 3-10 是 int 属性而不是布尔属性? (对于带有“Is”前缀的属性来说,这有点奇怪。)
  • @JonSkeet:我发布了错误的项目属性类型。现在已编辑,所有都是布尔值。
  • 使用了 linq 查询,但我得到的每个项目值不像基于匹配值获取所有项目。
  • 恐怕我不明白评论,但您应该确切地向我们展示您尝试了哪些 LINQ 查询,以及您的预期和发生了什么,在minimal reproducible example。请非常具体 - 只是说您尝试了“LINQ 查询”并没有为我们提供足够的信息来帮助您。
  • @JonSkeet,我添加了迄今为止我尝试过的 linq 查询,并对我的实际需求发表评论。

标签: list linq c#-4.0


【解决方案1】:

检查这个小提琴 https://dotnetfiddle.net/kjvWPC

    List<FilterControl> listFilters = new List<FilterControl>(){
   new FilterControl() { IsItem1 = false, IsItem2 = false, IsItem3 = true, IsItem4 = false, IsItem5 = true, IsItem6 = true, IsItem7 = false, IsItem8 = true, IsItem9 = false }};

    var getAllItemsWchValueTrue  =  listFilters.Select(p=>
            {

         List<string> items =new List<string>();
        foreach (PropertyInfo prop in p.GetType().GetProperties()){
         var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
         if (type == typeof (System.Boolean) && (bool)prop.GetValue(p, null))
         { 
            items.Add(prop.Name);
         }
        }

        return items;
       }).SelectMany(q=> q);

更整洁的方式https://dotnetfiddle.net/6dxa17

var getAllItemsWchValueTrue = listFilters.Select(p =>
        {
            return p.GetType().GetProperties().Where(prop => (prop.PropertyType == typeof (System.Boolean)) && (bool)prop.GetValue(p, null)).Select(prop => prop.Name);
        }

        ).SelectMany(q => q);

【讨论】:

  • 我可以像我在 JonSkeet 上评论的那样进行查询吗?
  • @V.Prasad 我明白你的意思你只想要项目名称吗?
  • 是的 Warmiq。我需要那些价值为真的物品。
  • 很棒的答案 Wamiq,这就是我想要的。感谢您理解我的方案。
【解决方案2】:

我相信Enumerable.Where 就是您要找的。您可以提交 lambda 表达式作为参数以按特定属性进行过滤。

【讨论】:

  • 我已经尝试过在哪里我可以让每个项目都具有价值。我需要这里的解决方案,比如基于 value=true 的一次性解决方案,相关项目应该从列表中分离出来。
猜你喜欢
  • 2018-10-07
  • 2013-11-26
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 2011-11-22
  • 2013-07-22
相关资源
最近更新 更多