Only do Where condition if a value is passed in

后端 未结 4 519
隐瞒了意图╮
隐瞒了意图╮ 2021-01-24 06:22

I have the following LINQ statement that does on where on the date and a LabID.

I\'m passing in a list of LABS and a date, however

4条回答
  •  一向
    一向 (楼主)
    2021-01-24 06:57

    You need to check if your values are null before doing the query, and if they are, don't do the extra condition.

    List lstDatExp = 
        (from l in ctx.dExp.Include("datLab")
         where 
             (values == null || values.Contains(l.datL.Lab_ID)) &&
             (reportingPeriod == null || l.reportingPeriod == reportingPeriod)
         select l).ToList();
    

    This way if values or reportingPeriod are null they are essentially optional.

提交回复
热议问题