I‘ve been working on a query using LINQ but I’ve run into a snag with a dynamic where clause. I want to check for a condition and if true then add that where to my query.
the problem is your order, at the select you are discarding all data not in the select statement, if you use a Inline if statement then you can easily do it in the first where clause or you can separate the select out after though you may have to convery from a Linq query to linq statments
var query = from project in db.ProjMasters
join pd in db.ProjDetails on project.ProjMasterID equals pd.ProjMasterID
join dc in db.DivCodes on project.DivisionCode equals dc.DivCode1
join ec in db.EmpCodes on project.ProjManager equals ec.UserNm
join ptc in db.ProjTypeCodes on pd.ProjTypeCode equals ptc.ProjTypeCode1
join psc in db.ProjStatusCodes on pd.ProjStatusCode equals psc.ProjStatusCode1
where pd.ProjDeleteDate == null
&& sProjType!= null ? ptc.TypeDesc==sProjType): true
orderby project.Title
select new
{
project.ProjMasterID,
project.Title,
pd.ProjDesc,
pd.ContractNum,
pd.ProjDetailID,
dc.DivNm,
}
if (sTitle != null)
{
query = query.Where(x => x.Title.Contains(sTitle));
}