filtering objects by inner collection property value - LINQ, C#

Jonas Lubys 1 Reputation point
2022-05-21T18:08:54.097+00:00

I haven't used LINQ for a while and things got forgotten.

How would one filter panelInfoModels by Exists value? The query below returns the same collection (does not apply intended filtering).

var q = (from p in panelInfoModels
                            from u in p.UUTInfo
                            where u.Exists == false
                            select p).ToList();

public class PanelInfoModel
{
    public string Sn { get; set; }
    public List<SubPanelInfoModel> SubPanelInfo { get; set; }
    public List<UUTInfoModel> UUTInfo { get; set; }
}


public class SubPanelInfoModel
{
    public string Sn { get; set; }
    public int Sequence { get; set; }
    public List<UUTInfoModel> UUTInfo { get; set; }
}


public class UUTInfoModel
{
    public string Sn { get; set; }
    public int Sequence { get; set; }
    public bool Exists { get; set; } = false;
}

Thanks

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2022-05-21T20:28:15.897+00:00

    Check if this query corresponds to guessed expectations:

    var q = panelInfoModels.Where( p => p.UUTInfo.All( i => !i.Exists ) ).ToList( );

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.