linq返回在嵌套列表中的子对象属性上过滤的对象列表<>[英] LINQ to return list of Object filtered on a property of a Child object in nested List<>

本文是小编为大家收集整理的关于linq返回在嵌套列表中的子对象属性上过滤的对象列表<>的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在寻求一些帮助,以 linq 查询以在嵌套列表中的自定义对象的属性/枚举上过滤,并希望在返回列表中维护父对象.

例如/clarity/示例代码,我有一个父对象,它的列表中的列表基于类和枚举:

public class Stage {
  public String Name { get; set;}
  public List<Evaluation> MyEvaluations { get; set;}
}
public class Evaluation {
  public float Result { get; set; }
  public enumResultType ResultType { get; set; }
}
public enum enumResultType { 
A,B,C 
}

曾经可以用以下内容沿这些行模拟样本数据:

List<Stage> ParentList = new List<Stage>();
Stage Stage1 = new Stage() { Name = "Stage1", 
MyEvaluations = new List<Evaluation>() { 
new Evaluation() { ResultType = enumResultType.A, Result=5 },
new Evaluation() { ResultType = enumResultType.B, Result=10},
new Evaluation() { ResultType = enumResultType.B, Result=11}, 
new Evaluation() { ResultType = enumResultType.C, Result=5}
}};
Stage Stage2 = new Stage() { Name = "Stage2",
MyEvaluations = new List<Evaluation>() { 
new Evaluation() { ResultType = enumResultType.A, Result=10},
new Evaluation() { ResultType = enumResultType.B, Result=20},
new Evaluation() { ResultType = enumResultType.C, Result=20}}};
ParentList.Add(Stage1);
ParentList.Add(Stage2);

我想通过 linq 从Parentlist对象中进行选择,所有的项目仅带有过滤列表,其中评估列表中的ResultType ResultType符合适当的列表健康)状况... 我不想多次重复父对象(SEED selectmany),而不是MyEvaluations的过滤列表,其中ResultType匹配,如果此列表具有项目(它将)父母.

我玩过:

ParentList.Select(x => x.MyEvaluations.FindAll(y => y.ResultType==enumResultType.B)).ToList();

但是,此仅返回内部列表...而

ParentList.Where(x => x.MyEvaluations.Any(y => y.ResultType==enumResultType.B)).ToList();

返回任何..但是我缺少如何获取要过滤的MyEvaluations的列表.

在我的示例/示例数据中,我想查询查询父母列表,以查询所有情况,其中resultype = enumresultType.b;

因此,希望获得相同类型的列表,但没有等于ResultType.A或.C

的"评估"

基于虚拟数据,我期望得到某些东西:

returnlist.count() - 2个项目(stage1/stage2)以及该阶段1-> foreach(item.Result:10,11 stage2-> foreach(item.Result:20

)

这可以在不参加新的匿名类型的预测的情况下完成此操作,因为我想保持列表的良好和清洁,如稍后使用的数据,我会仔细阅读许多resultytypes吗?

感觉我缺少一些相当简单的东西,但相当新的 linq 和lambda表达式.

推荐答案

您是否已经尝试了这些方法?还是这不是您要寻找的?

//creating a new list 
var answer = (from p in ParentList
             select new Stage(){
             Name = p.Name,
             MyEvaluations = p.MyEvaluations.Where(e => e.ResultType == enumResultType.B).ToList()
             }).ToList();

//in place replacement               
ParentList.ForEach(p => p.MyEvaluations = p.MyEvaluations.Where(e => e.ResultType == enumResultType.B).ToList());

本文地址:https://www.itbaoku.cn/post/1556819.html

问题描述

I'm looking for some help with a LINQ query to filter on a property/enum of a custom object which is in a nested List, and want to maintain the parent object in return list.

For example/clarity/sample code, I have a parent object, which has in it a List based on class and enum below:

public class Stage {
  public String Name { get; set;}
  public List<Evaluation> MyEvaluations { get; set;}
}
public class Evaluation {
  public float Result { get; set; }
  public enumResultType ResultType { get; set; }
}
public enum enumResultType { 
A,B,C 
}

Once can simulate sample data along those lines with something like:

List<Stage> ParentList = new List<Stage>();
Stage Stage1 = new Stage() { Name = "Stage1", 
MyEvaluations = new List<Evaluation>() { 
new Evaluation() { ResultType = enumResultType.A, Result=5 },
new Evaluation() { ResultType = enumResultType.B, Result=10},
new Evaluation() { ResultType = enumResultType.B, Result=11}, 
new Evaluation() { ResultType = enumResultType.C, Result=5}
}};
Stage Stage2 = new Stage() { Name = "Stage2",
MyEvaluations = new List<Evaluation>() { 
new Evaluation() { ResultType = enumResultType.A, Result=10},
new Evaluation() { ResultType = enumResultType.B, Result=20},
new Evaluation() { ResultType = enumResultType.C, Result=20}}};
ParentList.Add(Stage1);
ParentList.Add(Stage2);

What I want to be able to do via LINQ is to select from the Parentlist object, all the items with only a filtered list where The ResultType in the Evaluations List matches a proper condition... I don't want to repeat the parent object Multiple times (seen selectmany), rather a filtered down list of the MyEvaluations where the ResultType matches, and if this list has items (it would) return it with the parent.

I've played with:

ParentList.Select(x => x.MyEvaluations.FindAll(y => y.ResultType==enumResultType.B)).ToList();

however this return only the inner list... whereas

ParentList.Where(x => x.MyEvaluations.Any(y => y.ResultType==enumResultType.B)).ToList();

returns ANY.. however I am missing how to get the list of MyEvaluations to be filtered down..

In my Example/sample data, I would like to query query ParentList for all situations where ResultType = enumResultType.B;

So would expect to get back a list of the same type, but without "Evaluation" which are equal to ResultType.A or .C

Based on dummy data, I would expect to be getting something which would have:

returnList.Count() - 2 items (Stage1 / Stage2) and within that Stage1 --> foreach (item.Result : 10, 11 Stage2 --> foreach (item.Result : 20

Can this be done without going to projections in new anonymous types as I would like to keep the list nice and clean as used later on in DataBinding and I iterate over many ResultTypes?

Feel like I'm missing something fairly simple, but fairly new to LINQ and lambda expressions.

推荐答案

Did you try these approaches already? Or is this not what you're looking for ?

//creating a new list 
var answer = (from p in ParentList
             select new Stage(){
             Name = p.Name,
             MyEvaluations = p.MyEvaluations.Where(e => e.ResultType == enumResultType.B).ToList()
             }).ToList();

//in place replacement               
ParentList.ForEach(p => p.MyEvaluations = p.MyEvaluations.Where(e => e.ResultType == enumResultType.B).ToList());