Anup
Anup

Reputation: 9738

Linq - Dynamic Condition

I have the following query :-

I want to add one more condition which is dynamic, so if user passes DATEOFBIRTH it should be e.DateOfBirth <= date.

var data = ctx.Employee.Where(e => e.Id == Id
                                && e.Category == Category
                                && e.DateOfJoining <= date)
                                .Select(e => e)
                                .ToList();

How to condition dynamically?

Upvotes: 1

Views: 391

Answers (3)

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

You can use reflection to solve this problem but there is another idea that may helps you:

  var criteria = new Dictionary<string, Func<Employee, bool>>();

  var date = DateTime.Now;  //or any other value

  //Initialize your criterias   
  criteria.Add("DATEOFBIRTH", e => e.DateOfBirth <= date);
  criteria.Add("DateOfJoining", e => e.DateOfJoining <= date);

  var selectedValue = "DATEOFBIRTH";

  var data = ctx.Employee.Where(e => e.Id == id &&
                                     e.Category == Category &&
                                     criteria[selectedValue](e)).ToList();

So if you change the selectedValue the output will be based on corresponding criteria you are looking for.

Upvotes: 1

JLRishe
JLRishe

Reputation: 101652

Sounds like you're trying to do the following:

var employees = ctx.Employee.Where(e => e.Id == Id
                                        && e.Category == Category
                                        && e.DateOfJoining <= date);

if (!string.IsNullOrWhiteSpace(DATEOFBIRTH))
{
    employees = employees.Where(e => e.DateOfBirth <= DATEOFBIRTH);
}

var data = employees.ToList();

You could also do the following, which is more concise, but since it looks like you are querying a database here, I would prefer the above approach since it doesn't include anything unnecessary in the query.

var data = ctx.Employee.Where(e => e.Id == Id && 
                                   e.Category == Category && 
                                   e.DateOfJoining <= date &&
                                   (string.IsNullOrWhiteSpace(DATEOFBIRTH) ||
                                    e.DateOfBirth <= DATEOFBIRTH))
                       .ToList();

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460038

From your comment:

If the DateOfBirth is choosen, there where condition should be appended by one more condition e.DateOfBirth <= date.. if user chooses DateOfAnniversary then it should be e.DateOfAnniversary <= date

Then you could use:

var data = ctx.Employee
    .Where(e => e.Id == Id && e.Category == Category && e.DateOfJoining <= date);

Now, assuming that filterbyDateOfBirth and filterbyDateOfAnniversary are bools:

if(filterbyDateOfBirth)
    data = data.Where(e => e.DateOfBirth <= date);
if(filterbyDateOfAnniversary)
    data = data.Where(e => e.DateOfAnniversary <= date);

var list = data.ToList();

Due to LINQ's deferred execution the database is queried just once at ToList.

Upvotes: 0

Related Questions