question

GaniTPT avatar image
0 Votes"
GaniTPT asked GaniTPT commented

How to handle null values in LINQ select statement in C#

I am getting Object reference error. because of null values.

How to handle null values in LINQ Select statement

string Qtyvalues = dtQty.AsEnumerable().Select(p => p.Field<string>("KEYMATERIALS").ToString()).FirstOrDefault();

Error : object reference.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered GaniTPT commented

Try this or a variation

 var Qtyvalues = dtQty
     .AsEnumerable()
     .Where(x => x["COL3"] != DBNull.Value)
     .Select(p => p.Field<string>("QTY"));
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

.Where(x => x["COL3"] != DBNull.Value) ==.> what is this..? i haven;t mentioned "COL3" anywhere...?

0 Votes 0 ·

i think it is,

.Where(x => x["QTY"] != DBNull.Value) ==> It's working..

Thanks..

0 Votes 0 ·