1

I have a list which contains multiple time frames. Each time frame is a timeframe class instance.

public class TimeFrame
{
    public DateTime From { get; set; }
    public DateTime To { get; set; }
}

I want to filter some entity by DateTime CreationDateTime field with WHERE clause using all timeframes in my list combined with OR.

Something like this

SELECT 
    * 
FROM 
    Entities
WHERE 
    ((T1-From <= CreationDateTime) AND (CreationDateTime <= T1-To))
    OR ((T2-From <= CreationDateTime) AND (CreationDateTime <= T2-To))
    OR ((TN-From <= CreationDateTime) AND (CreationDateTime <= TN-To))

How can i do it using entity framework version 6?

Andrey Dengin
  • 181
  • 2
  • 15

1 Answers1

0

Using PredicateBuilder in LINQKit you could do something like this:

IQueryable<Entity> SearchEntities (List<TimeFrame> times)
{
  var predicate = PredicateBuilder.False<Product>();

  foreach (string time in times)
  {
    TimeFrame temp = time;
    predicate = predicate.Or (p => (temp.From <= p.CreationDateTime) AND (p.CreationDateTime <= temp.To));
  }
  return dataContext.Entities.AsExpandable().Where (predicate);
}
Geoff Appleford
  • 18,538
  • 4
  • 62
  • 85