Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Monday, 2 July 2018

LINQ query check if not null then only apply condition

Sometimes, we have requirement like need to pass one parameter but its nullable, and we have to pass this parameter in where clause also need to check only if value is not null then only check condition..then in this case what we can do ..???

well here is one of the solution :

public employee GetEmployee(int id, string appId)
{
      var result= (from e in objcontext.employee
                         where e.Id==id &&
                         (string.IsNullOrEmpty(appId) || e.AppId==appId)  
// Here it will check first if null then it will not go to second condition else it will check where clause for that column  
                          select e).ToList();

}

Thursday, 22 January 2015

Check whether one list contain another list value in LINQ


var CheckExist = List1.Any(l1 => List2.Any(l2 => l1.Name == l2.Name));


Here CheckExist  object will return true if any record of List1 contain in List2