Thursday, 16 January 2020

filter collection from multiple values

Suppose we have a list MyList
Then we can take in enumerable like below

IEnumerable<TestObject> query = MyList;
if (obj1 != null) {
    query = query.Where(x => x.Id == obj1.ID);
}
if (obj2= null) {
    query = query.Where(x => x.Number.Contains(obj2.Number));
}
if (obje3 != null) {
    query = query.Where(x => x.Date == obj3.Date);
}
......
you can use many more
Finally we can convert in list, It basically reduce the complexcity

MYListView = query.ToList();

Friday, 27 December 2019

ternary operator in C#

 this is a ? : Ternary Operator
it is return one of two values depending on Boolean condition 
Syntax:
condition ? expressionIfTrue : expressionIfFalse;
Example:
string name = "hamid";
Console.WriteLine(name == "hamid" ? "The name is hamid" : "The name is not hamid");

so it will return : The name is hamid

Console.WriteLine(name == "hamidXYZ" ? "The name is hamid" : "The name is not hamid");
so it will return : The name is not hamid