Search code examples
c#linqlinq-to-entities

Use new DateTime in LINQ Queries Where Clause


I have a database here in which, for reasons I cannot understand, the year and the month for the validity of an entry are stored in TWO separate fields...

YearPart=2023
MonthPart=2

Now I need to create a LINQ query to retrieve data based on a DateTime input...

In LINQ Pad such things are possible:

var data2 = data.Where(t => new DateTime(t.DtmZlpServiceIBS.YearPart, t.DtmZlpServiceIBS.MonthPart, 1) <= fromDate);

Does anyone have any idea how I can implement this?


Solution

  • This should be translatable:

    var fromYear = fromDate.Year;
    var fromMonth = fromDate.Month;
    
    var data2 = data.Where(t =>
        t.DtmZlpServiceIBS.YearPart < fromYear ||
        (t.DtmZlpServiceIBS.YearPart == fromYear && t.DtmZlpServiceIBS.MonthPart <= fromMonth));