0

Apology first, I am really beginner with the LINQ, hence the question.

I am being unable to catch values that are null form the database. I am making a LINQ to DataTable. I am checking the double type.

This is the code I have:

var results2 = from myRow in table.AsEnumerable()
                                       where myRow.Field<double>("Cost") == 0
                                       select myRow;

That Code works fine if a value is zero. How Do I make it check for null as well?

Another question is also related to this issue:

I have this code:

var results = from myRow in table.AsEnumerable()
                                       where myRow.Field<string>("Name") == "test"
                                       select myRow;

And that works just fine if the value is test. How do I check if the "Name" column is empty/null??

3
  • myRow.Field<double?>("Cost") == null? Commented Oct 5, 2015 at 14:19
  • Why do you use .AsEnumerable()? Are you sure this is Linq to SQL? Why not use a typed data context? Commented Oct 5, 2015 at 14:22
  • I am running this LINQ against the DataTable. Commented Oct 5, 2015 at 14:25

1 Answer 1

2

Add an OR condition with the || operator.

For your nullable double case, you will need to check if it has a value AND is the value you expect, in addition to checking for null.

var results2 = from myRow in table.AsEnumerable()
               where (
                   myRow.Field<double?>("Cost").HasValue &&
                   myRow.Field<double?>("Cost").Value == 0)
               || !myRow.Field<double?>("Cost").HasValue
               select myRow;


var results = from myRow in table.AsEnumerable()
              where myRow.Field<string>("Name") == "test" ||
                    myRow.Field<string>("Name") == null
              select myRow;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.