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

查看:91
本文介绍了LINQ返回在嵌套List的子对象的属性中过滤的对象的列表.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关 LINQ 查询的帮助,以对嵌套列表中的自定义对象的属性/枚举进行过滤,并希望将父对象维护在返回列表中.

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

我想通过 LINQ 进行的操作是从Parentlist对象中选择所有带有仅筛选列表的项目,其中评估列表"中的ResultType匹配适当的健康)状况... 我不想重复父对象多次(见selectmany),而是MyEvaluations匹配的MyEvaluations的过滤列表,并且如果此列表中有项目(它将)与父母.

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.

我玩过:

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

返回ANY ..但是我不知道如何获取要过滤的MyEvaluations列表..

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

在我的示例/示例数据中,我想查询ResultType = enumResultType.B;的所有情况的查询ParentList.

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

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

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个项目(第1阶段/第2阶段),并且在该Stage1中-> foreach(项目结果:10,11 Stage2-> foreach(项目结果:20

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

是否可以不使用新的匿名类型进行投影来完成操作,因为我想保持列表的美观和整洁(如稍后在DataBinding中使用的那样,并且我要遍历许多ResultTypes?)

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?

感觉我缺少了一些相当简单的东西,但是对于 LINQ 和lambda表达式来说却是相当新的东西.

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

这篇关于LINQ返回在嵌套List的子对象的属性中过滤的对象的列表.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆