I need to write a linq that give me max DetailRoot EventDate Where DetailModel Color equals "Red". Review structure below
Dictionary
public class DetailRoot
{
public string UserId{ get; set; }
public string Session{ get; set; }
public Datetime EventDate { get; set; }
public IReadOnlyList
}
public class DetailModel
{
public string ApplicationGuid { get; set; }
public string ColorType { get; set; }
}
Simone CappellettiPosted Apr 20, 2022, 7:54 PM
David SmithPosted Apr 20, 2022, 1:13 PM
As the linq is querying through hundreds of records I get an error Sequence contain no elements how to get pass that error even if that's true. I don't won't that error happening in the middle of that run. Is there away to modify the linq to just return empty or 0 if case is zero
Simone CappellettiPosted Apr 20, 2022, 8:06 AM
Hello, try something like:
var query = from source in dataList.AsEnumerable()
let x = source.Value.Where(y => y.DetailList.Any(z => z.ColorType.Equals("Red"))).ToArray()
//where
select x.Max(y => y.EventDate);
I saved the list in a variable named x for convenience and that you can reuse in the clause where to add some logic.
Mario ScheerenPosted Apr 20, 2022, 7:26 AM
var date = dataList.Where(x => x.DetailList.Any(y => y.ColorType == "Red")).Max(x => x.EventDate);