简体   繁体   English

c# LINQ 过滤器嵌套集合

[英]c# LINQ filter nested collection

I have the next class hierarchy:我有下一个 class 层次结构:

public class URecordFieldEmr
{
   public Guid Key { get; set; }
   public string Alias { get; set; }
   public List<Value> Values { get; set; }
}

public class Value
{
    public int Index { get; set; }
    public List<DayOfWeek> DaysOfWeek { get; set; } 
}

I need to filter List<URecordFieldEmr> by day of week, so I'm trying like this:我需要按星期几过滤List<URecordFieldEmr> ,所以我正在尝试这样:

var filtredExercises = uRecordFields
                         .Where(f => f.Values.Any(v => v.DaysOfWeek.Contains(dayOfWeek)))
                         .ToList();

But in this case if I have two Value s: first value - DaysOfWeek - sunday, second value - monday, and when I'm trying filtering by sunday I get both these values.但在这种情况下,如果我有两个Value :第一个值 - DaysOfWeek - 星期日,第二个值 - 星期一,当我尝试按星期天过滤时,我得到这两个值。 But I need values that contains specific day of week.但我需要包含特定星期几的值。

PS dayOfWeek is .NET enum PS dayOfWeek 是 .NET 枚举

The Question is, why do you even have a List of dayofweek in class values if the match can only be exaclty one day week?问题是,如果匹配只能是精确的一周,为什么你甚至在 class 值中有一个星期列表? Shouldn't value then just look like this?那么价值不应该看起来像这样吗?

public class Value
{
    public int Index { get; set; }
    public DayOfWeek d { get; set; } 
}

Or do you wannt to be able also to check if List<DayOfWeek> DaysOfWeek contains the exact same combination as you want to compare to, for example Monday and Sunday, then the list needs to contain exactly and only Monday and sunday?或者您是否还希望能够检查List<DayOfWeek> DaysOfWeek是否包含与您想要比较的完全相同的组合,例如周一和周日,那么列表需要准确且仅包含周一和周日? Or Wednesday and Friday and Thurstday then the list only must contain these values and all 3 need to be present?或者周三、周五和周四,那么列表只必须包含这些值并且所有 3 个都需要存在?

Anyhow, in case of that you only need to compare one entry and it should be the only entry in the list (as you described it), either rewrite the values class as described above.无论如何,如果您只需要比较一个条目并且它应该是列表中的唯一条目(如您所描述的那样),请如上所述重写值 class 。 Or you could check it like this:或者你可以像这样检查它:

var filtredExercises = uRecordFields
                         .Where(f => f.Values.Any( v => v.DaysOfWeek.count = 1 && v.DaysOfWeek.FirstOrDefault() == dayofweek))
                         .ToList();

Maybe this approach is more readable.也许这种方法更具可读性。 You also could add distinct() before firstordefault() if your class allowes multiple entries of the same day.如果您的 class 允许同一天的多个条目,您也可以在 firstordefault() 之前添加 distinct()。

You also could do a method on the class values that does the check for you and then you call that method in the lambda.您还可以对 class 值执行一个方法来为您进行检查,然后在 lambda 中调用该方法。 But this depends if the natural responsibility for the check is in the class itself.但这取决于检查的自然责任是否在 class 本身。

What I assume is, you need to filter the matched Values property too right?我假设的是,您需要过滤匹配的Values属性对吗? If so the below query may help如果是这样,以下查询可能会有所帮助

var result = uRecordFields.Select(x => new URecordFieldEmr
{
    Values = x.Values.Where(y => y.DaysOfWeek.Any(z => z == DayOfWeek.Sunday)).ToList(),
    Alias = x.Alias,
    Key = x.Key
}).Where(x => x.Values != null && x.Values.Count != 0).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM