I'm trying to build a complex query with linq.
I have a C# method that takes in parameters some arguments:
public void function returnAList(string arg1, string arg2,
string arg3, string arg4);
I put the following code that lets me return a list from my database:
List<Person> listOfPersons = this.businessLayer.ReturnPersons();
var persons= from p in listOfPersons
select new
{
FirstName = p.FirstName,
LastName = p.LastName,
Age = p.Age,
Address = p.Address,
PhoneNumber = p.PhoneNumber
};
What I want, is to inject my args in the query so that I can obtain the following algorithm:
List<Person> listOfPersons = this.businessLayer.ReturnPersons();
var persons= from p in listOfPersons
where (
if (arg1 isNotNull)
(p.FirstName == arg1)
if (arg2 usNotNull)
(p.LastName == arg2)
...
)
select new
{
FirstName = p.FirstName,
LastName = p.LastName,
Age = p.Age,
Address = p.Address,
PhoneNumber = p.PhoneNumber
};
Is there a way to do that?