2

I have a table (I am using Entity Model) and filtered that table using LINQ query which is working fine. Now I want to filter the records on the basis of array of dates. I am not able to implement IN clause on array of dates

filteredList = leadsNewAndScheduled.Where(lead =>  
              (LeadTypeIDs.Contains(lead.TYPE_ID.ToString()) ||
              LeadTypeIDs == string.Empty) &&
              (priorityIDs.Contains(lead.PRIORITY_ID.ToString()) ||
              priorityIDs == string.Empty) &&
              (((lead.EXPIRED_ON <= dateExpiry ||
              Convert.ToDateTime(lead.EXPIRED_ON) == DateTime.Today.Date) &&
              lead.STATUS_ID == (int)Enumerations.LeadStatus.New) ||
              lead.STATUS_ID == (int)Enumerations.LeadStatus.Active) &&
              (lead.START_TIME IN (arrAppointmentDates))
            ).ToList();

I want your help in following

(lead.START_TIME IN (arrAppointmentDates))

Thanks in advance.

2 Answers 2

2

Use Predicate Builder

Write your query without the date condition like

var query = leadsNewAndScheduled.Where(lead =>  
              (LeadTypeIDs.Contains(lead.TYPE_ID.ToString()) ||
              LeadTypeIDs == string.Empty) && ....

Then write

  var predicate = PredicateBuilder.False<Lead>();

  foreach (DateTime date in dates)
  {
    DateTime temp = date;
    predicate = predicate.Or (p => p.START_TIME == temp);
  }

  var result = query.Where(predicate).ToList(); // Don't call ToList() earlier

However please note that if you're using Entity Framework you need to call AsExpandable() on the entity set before applying predicates on it like so:

return objectContext.Products.AsExpandable().Where (predicate);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks hasan, I created a class PredicateBuilder from your given source and implemented following code; var predicate = PredicateBuilder.False<LeadGetNewAndScheduledByEmployee_type>(); foreach (DateTime date in arrAppointmentDates) { DateTime temp = date; predicate = predicate.Or(p => Convert.ToDateTime(p.START_TIME).Date == temp); } filteredList = filteredList.Where(predicate).ToList(); But it gives me following error 'System.Collections.Generic.List<EntityModel.LeadGetNewAndScheduledByEmployee_type>' does not contain a definition for 'Where'. Any help in this ...
Do you have System.Linq namespace included?
Yes I have Included System.Linq
What is the type of leadsNewAndScheduled and where it is coming from?
leadsNewAndScheduled is of complex type Of Entity Model (i.e. List<LeadGetNewAndScheduledByEmployee_type>()) which is an output from a MSSQL Stored Procedure.
1

I solved this problem while declaring list of dates and then applying contains clause in LINQ query.

example:

//list of dates.
List<DateTime> arrAppointmentDates;

//change in query
arrAppointmentDates.Contains(Convert.ToDateTime(lead.START_TIME).Date)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.