Linq dynamic query (Except - but SQL SERVER side)

c# entity-framework-core linq sql-server

Question

I want to return records where the date column is greater than DateTime.Now.AddDays(-3), but only if the type column is complete. Meaning, I want to return everything except for records that are older than 3 days and the type is == complete (Enum 1).

It's almost as if I need Except, but for that, I'd need to enumerate the results, then I wouldn't have my page number of results (.Take(10)).

How would this be done in linq? I can't figure out how this query would look. I would like the query to be done SQL SERVER side as much as possible as to be efficient.

Just to be clear - I want all results, EXCEPT records that are completed and older than 3 days.

(completed meaning the type column value == 1 (Enum))

My project is .NetCore 3.1

Thanks

1
-1
2/6/2020 1:20:07 PM

Accepted Answer

Could be this that you need ?

  Var results = context.Entity.Where(x=> x.Type != enum.Completed ||( x.Type == enum.Completed && x.Date >=DateTime.Now.AddDays(-3)));
0
2/6/2020 3:34:54 PM

Popular Answer

Maybe something like this?

Results = context.Entity.Where(x => x.Date > DateTime.Now.AddDays(-3) || x.Type != YourEnum.Complete);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow