Answered by:
LINQ If statement within a where clause
Question

Hi Experts,
I am trying to include an if statement with in a where clause, meaning I have to check if the criteria is supplied or not.
It should be similar to SQL where one would say
Where Column = ISNULL(@Parameter,Column)
Please refer to my code block below. Your help is highly appreciated.
var LinqProjects = (from p in Projects join ps in ProjectStatus on p.StatusID equals ps.StatusID join c in Customer on p.CustomerID equals c.CustomerID //error here where ps.StatusDescription = (ActiveOnly == true ? "Open" : ps.StatusDescription.ToString()) orderby p.ProjectName select new {c.CustomerName ,p.ProjectName, p.ProjectDescription, p.ProjectID, p.StartDate, p.EndDate, p.ProjectCode, p.CreationDate ,p.CreatedBy ,p.ModificationDate , p.ModifiedBy,p.StatusID,ps.StatusDescription }).Distinct();
Tuesday, February 1, 2011 2:17 PM
Answers

Sorry, but I don't get your point...
First of all the equation ps.StatusDescription == ps.StatusDescription.ToString() doesn't make a lot of sense to me...
However, if you combine (AND) your logic you get:
Where (ActiveOnly == true AND ps.StatusDescription == "Open") OR
(ActiveOnly == false AND ps.StatusDescription == ps.StatusDescription.ToString())
In case ActiveOnly is true, the second part of the first statement has to be true as well. In case ActiveOnly is false, the seconde part of the second statement has to be true...
 Proposed as answer by Sheldon _Xiao Wednesday, February 9, 2011 2:29 AM
 Marked as answer by Sheldon _Xiao Wednesday, February 9, 2011 2:31 AM
Tuesday, February 1, 2011 3:14 PM
All replies

where (ActiveOnly == true && ps.StatusDescription == "Open") 
(ActiveOnly == false && ps.StatusDescription == ps.StatusDescription.ToString())
Tuesday, February 1, 2011 2:48 PM 
Thanks Dunken,
But actually the logic is like
(Assuming that the value of the ActiveOnly is set by the ActiveOnly Checkbox.checked value.)
If (ActiveOnly == true)
{
//Do this where statement
Where ps.StatusDescription == "Open"
}
else
{
//Do this where clause
Where ps.StatusDescription == ps.StatusDescription.ToString()
}
Tuesday, February 1, 2011 2:59 PM 
Sorry, but I don't get your point...
First of all the equation ps.StatusDescription == ps.StatusDescription.ToString() doesn't make a lot of sense to me...
However, if you combine (AND) your logic you get:
Where (ActiveOnly == true AND ps.StatusDescription == "Open") OR
(ActiveOnly == false AND ps.StatusDescription == ps.StatusDescription.ToString())
In case ActiveOnly is true, the second part of the first statement has to be true as well. In case ActiveOnly is false, the seconde part of the second statement has to be true...
 Proposed as answer by Sheldon _Xiao Wednesday, February 9, 2011 2:29 AM
 Marked as answer by Sheldon _Xiao Wednesday, February 9, 2011 2:31 AM
Tuesday, February 1, 2011 3:14 PM 
Hi AlpheusA,
I think Dunken has provided you some helpful information, so I am marking his reply as the answer of your thread, if your issue persist or you have new findings about your issue, please unmark.
Best regards,
Sheldon _Xiao [MSFT]
MSDN Community Support  Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Wednesday, February 9, 2011 2:30 AM 
Thanks Dunken, This is exactly what I was looking for.
Wednesday, February 9, 2011 11:05 AM