-2

The Where clause has at least 1 Datarow as a result as I know for sure.

And I checked != null before calling 1) Select 2) DefaultIfEmpty method of course and it passed without a problem.

I also tried to use ?? false but don't know the exact mechanism it works and how to use it.

The columns are all Nullable.

I don't know what is wrong here...looking for solution all day.. Because I'm self-learning programmer, I'm really lack of fundamental knowledge.

dt.AsEnumerable().Where(dr => (dr.Field<string>("name_first_API").Contains("Ibuprofen")
                            || dr.Field<string>("name_first_API").Contains("Dexibuprofen")
                            || dr.Field<string>("name_first_API").Contains("Naproxen")
                            || dr.Field<string>("name_first_API").Contains("Meloxicam"))
                            && dr.Field<DateTime>("rx_visitdate").Date >= DateTime.Today.AddYears(-3)).
                  Select(dr => dr.Field<int>("howlongday_rx")).
                  DefaultIfEmpty(0).Sum();
ASh
  • 34,632
  • 9
  • 60
  • 82
Kay Lee
  • 922
  • 1
  • 12
  • 40
  • Either the DataTable or one of the fields are null – Tim Schmelter Jul 27 '18 at 07:31
  • @TimSchmelter, I also reviewed your answers for hours but cannot understand. The DataTable is not null for sure. Then how to avoid the fields are null ? Please kindly advise me. – Kay Lee Jul 27 '18 at 07:33
  • For example: `...Where(dr=>dr.Field("name_first_API")?.Contains("Ibuprofen")==true || ...)` – Tim Schmelter Jul 27 '18 at 07:35
  • @TimSchmelter, Ok let me try like this... – Kay Lee Jul 27 '18 at 07:36
  • @TimSchmelter, all are nullable as I wroted in OP. They are all from SQL table. – Kay Lee Jul 27 '18 at 07:37
  • @TimSchmelter, it works ! :) Because I know how StackOverflow works, Before asking, I've searched for all day with this but, now you're my 1 sec savior...Thank you so much ! – Kay Lee Jul 27 '18 at 07:45
  • For whom is looking for another solution, refer to HimBrombeere's answer. https://stackoverflow.com/questions/35134532/avoid-nullreferenceexception-in-linq-expression-on-datatable-column/35134640#35134640 – Kay Lee Jan 24 '22 at 08:38

1 Answers1

3

If all can be null you could use:

string[] meds = { "Ibuprofen", "Dexibuprofen", "Naproxen", "Meloxicam" };
int sum = dt.AsEnumerable()
    .Select(dr => new
    {
        name = dr.Field<string>("name_first_API"),
        visit = dr.Field<DateTime?>("rx_visitdate"),
        howlong = dr.Field<int?>("howlongday_rx")
    })
    .Where(x => meds.Contains(x.name, StringComparer.InvariantCultureIgnoreCase)
                && x.visit?.Date >= DateTime.Today.AddYears(-3))
    .Sum(x => x.howlong ?? 0);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Oh, Teacher...are you writing in lightspeed ? :) I feel uncomfortable with high-level code...You've been and be my teacher always..Thank you ! – Kay Lee Jul 27 '18 at 07:50
  • you're happiness provider. how about happiness business? :) – Kay Lee Jul 27 '18 at 07:53