简体   繁体   中英

Avoid NullReferenceException in LINQ Expression on Datatable column

I am stuck with null values in my Datatable "articles". Using LINQ to get a list of articles works for column ArticleId but with column "ArticleVariations" the null values are killing me.

var result = this.articles.AsEnumerable().Where(r =>r.Field<String>("ArticleId").Equals(artNo)); // works. no nulls there ;)

var result = this.articles.AsEnumerable().Where(r =>r.Field<String>("ArticleVariations").Equals(artNo)); // stuck with nulls here

If the column contains nulls, I get an NullReferenceException, Can I avoid this somehow and is it possible to merge both expressions?

You can use null-conditional and null-coalescing operators:

var result = this.articles.AsEnumerable()
                 .Where(r =>r.Field<String>("ArticleVariations")?.Equals(artNo) ?? false);

The problem obviously occurs because r.Field<String>("ArticleVariations") retuns null . Thus you have to check for null before calling Equals on it.

For that you can call multiple statements within a LINQ-expression:

var result = this.articles.AsEnumerable().Where(r => {
        var res = r.Field<String>("ArticleVariations");
        if (res != null) return res.Equals(artNo);
        else return false;
    });

If the field can be null then just reverse your test:

var result = this.articles.AsEnumerable().Where(r => artNo.Equals(r.Field<String>("ArticleVariations")));

Then all you need to do is check that artNo is not null before making the call:

List<Type> result;
if (string.IsNullOrWhiteSpace(artNo))
{
    result = new List<Type>();
}
else
{
    result = this.articles.... as before
}

Where just takes a function which returns a bool to determine if it should filter an item out of a collection. You can write it with a multi-statement body just like any other function to make nulls easier to deal with. Something like this should be a good starting point:

.Where(r => {
               string articleVariations = r.Field<string>("ArticleVariations");
               return articleVariations != null && articleVariations.Equals(artNo);
            });

IF you want to combine those checks somehow to build a list where one or the other of the given fields matches your artNo , you can just add it to the function body.

If the column contains nulls, I get an NullReferenceException, Can I avoid this somehow

Avoid using instance Equals methods where possible. Use the respective operators or static Equals methods because they handle correctly null s for you.

In your concrete case, the easiest way is to replace Equals with == :

var result = this.articles.AsEnumerable()
    .Where(r => r.Field<string>("ArticleId") == artNo);

var result = this.articles.AsEnumerable()
   .Where(r => r.Field<string>("ArticleVariations") == artNo);

and is it possible to merge both expressions?

It depends on what do you mean by "merging" them. If you mean matching article or variation by the passed artNo , then you can use something like this

var result = this.articles.AsEnumerable()
    .Where(r => r.Field<string>("ArticleId") == artNo
        || r => r.Field<string>("ArticleVariations") == artNo);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM