LINQ where clause with lambda expression having OR clauses and null values returning incomplete results

后端 未结 3 499
醉酒成梦
醉酒成梦 2021-02-02 17:36

the problem in short

we have a lambda expression used in the Where clause, which is not returning the \"expected\" result.

quick summary

3条回答
  •  旧巷少年郎
    2021-02-02 17:59

    You are checking Parent properties for null in your delegate. The same should work with lambda expressions too.

    List analysisObjects = analysisObjectRepository
            .FindAll()
            .Where(x => 
                (x.ID == packageId) || 
                (x.Parent != null &&
                    (x.Parent.ID == packageId || 
                    (x.Parent.Parent != null && x.Parent.Parent.ID == packageId)))
            .ToList();
    

提交回复
热议问题