I am trying to figure out why this LINQ query (C#) doesn't work as expected:
// If the user receives 3 salaries, take the first, if not, show as 100
var salary = populationData
.Select(
x => x.AdultData?
.TaxPayerData?
.Where(x => x.Salary.Count == 3)
.Select(x => x.Value).FirstOrDefault()
)
.DefaultIfEmpty(100)
.ToList();
PS I corrected the names of the properties, they were misleading, sorry.
Suppose for some people IsAdult = false
and TaxPayer = null
, for those people I need DefaultIfEmpty = 100
, or any other value I choose, but not null
. Currently, my result comes as null
. Any thoughts, please?
You can try this way
// If the user receives 3 salaries, take the first, if not, show as 100
var salary = populationData
.Select(
x => {
var result = x.AdultData?
.TaxPayerData?
.Where(x => x.Salary.Count == 3)
.Select(x => x.Value).FirstOrDefault()
return result ?? 100;}
)
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.