So I didn't see a question here that really answers this question. It's kinda a newbie question about linq but I would like to know if it would be possible to convert the following sql query (built using C#) into a linq query:
public void DoSomeQuery(bool whereCriteria1, bool whereCriteria2)
{
string sqlQuery = "SELECT p.*";
string fromClause = " FROM person p";
string whereClause = " WHERE ";
if (whereCriteria1)
{
fromClause += ", address a";
whereClause += " p.addressid = a.addressid and a.state = 'PA' and a.zip = '16127' "
}
if (whereCriteria2)
{
fromClause += ", color c";
whereClause += " p.favoritecolorid = c.colorid and c.name = 'blue'"
}
// arbitrarily many more criteria if blocks could be here
sqlQuery += fromClause + whereClause;
// do stuff to run the query
}
Does that make sense? I have a bunch of bool variables that let me know which where clause criteria to add. I want to do that in linq because well ... this is ugly.