Tuesday, July 7, 2015

c# - linq lambda expression and '&&' operator in where clause

Are you trying to apply “AND” operator in lambda expression.

Then below example will help you. If you are trying apply “AND” operator with where clause then you need to use the below syntax and that to be in where clause

public class ComplexClass
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string CellNumber { get; set; }

        public string Email { get; set; }
    }

List<ComplexClass> lstcomplexClass = new List<ComplexClass>();

resultItems = lstcomplexClass.Where(x => (x.Name == “Test”) && (x.Id== 1)).ToList();



And operator “&&” has to be applied in the Where clause. By applying the where you will get the item say “x”(in our example) from the list. Then system will apply the condition on the item to get the items from the list.