using a conditional operator to check for null inside LINQ query leads to ArgumentNullException during runtime RRS feed

  • Question

  • ok. say i have a linq query where i want to filter using a list, but only if that list is not null

    return (dataContext.SE_DETAIL_VIEWs.Where(
                        det =>
                        (projectFilter != null ? projectFilter.Contains(det.CI_PROJECT_ID) : true) &&
                        det.SE_MAIN.YEAR== startDate.Year &&
                        det.SE_MAIN.MONTH== startDate.Month &&
                        det.SE_MAIN.SIGNED && staffFilter.Contains(det.SE_MAIN.CI_PERSON.ID)).OrderBy(s => s.SE_MAIN.CI_PERSON.NAME)).ToList();

    this works fine during design time but fails during runtime and i cannot quite see why. the syntax looks correct to me. i want to avoid having to write two almost identical LINQ queries and check if my list is null with an if/else.

    the exception i get is System.ArgumentNullException was unhandled by user code
      Message="Value cannot be null.\r\nParameter name: source"

    any ideas?
    Thursday, January 28, 2010 12:28 PM

All replies