按特定值从通用列表中获取所有项目
Get all the Items from Generic List by Specific value
我有一个通用列表,其中有很多项。有些项目具有特定的值,例如 true,这里我需要 运行 一个 linq 查询,以使基于项目值 = 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();
但是在这段代码中,我只能检查一项是真还是假。
我需要在这里检查哪些项目具有 true 那些我想在 Var 变量中拥有的项目。
我相信 Enumerable.Where 就是您要找的。您可以提交 lambda 表达式作为参数以按特定 属性.
进行过滤
检查这个fiddle
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);
我有一个通用列表,其中有很多项。有些项目具有特定的值,例如 true,这里我需要 运行 一个 linq 查询,以使基于项目值 = 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();
但是在这段代码中,我只能检查一项是真还是假。 我需要在这里检查哪些项目具有 true 那些我想在 Var 变量中拥有的项目。
我相信 Enumerable.Where 就是您要找的。您可以提交 lambda 表达式作为参数以按特定 属性.
进行过滤检查这个fiddle 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);