How to write dynamic where clause for join range varible

前端 未结 3 634
感动是毒
感动是毒 2021-01-16 13:46

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.

3条回答
  •  没有蜡笔的小新
    2021-01-16 14:12

    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));
                 }
    

提交回复
热议问题