0

Good day. I'm trying perform select query with linq from collection of establishments. Establishment contains list of products it selling, but list of products can be empty. Well, I guess cause of that I get null exception. How I fix it and avoid null exception? This is my linq query:

var result = Establishments.Select(e => new
                {
                    ID = e.ID,
                    Name = e.Name,
                    Category = e.Category.Name,
                    XAdress = e.XAdress,
                    YAdress = e.YAdress,
                    CompanyName = e.Company.Name,
                    ProductsSelling = e.ProductsSelling.Select(p => new // error here
                    {
                        ID = p.ID,
                        Name = p.Name,
                        Category = p.Category.Name,
                        Price = p.Price,
                        Additives = p.PossibleAdditives.Select(a => new
                        {
                            ID = a.ID,
                            Name = a.Name,
                            Price = a.Price
                        })
                    })                    
                });
Amelina
  • 172
  • 1
  • 2
  • 12
  • 1
    Looks like `e.ProductsSelling` is null for some value of `e`. Your computer must be broken. – 15ee8f99-57ff-4f92-890c-b56153 Oct 07 '16 at 17:11
  • Most likely, `ProductsSelling` is null for some `Establishment`. There's not enough info to know why. – Gasper Oct 07 '16 at 17:14
  • what is data type of Establishments? if it is of type System.Collections.Generic.List where T is Establishment datatype, then you must check whether e.ProductsSelling is null or not null before expanding it. – sam Oct 07 '16 at 17:18

1 Answers1

3

You can check that ProductsSelling is not null before querying it using Null-Conditional Operator:

var result = Establishments.Select(e => new
            {
                ID = e.ID,
                Name = e.Name,
                Category = e.Category.Name,
                XAdress = e.XAdress,
                YAdress = e.YAdress,
                CompanyName = e.Company.Name,
                ProductsSelling = e.ProductsSelling?.Select(p => new /* note the '?' operator*/
                {
                    ID = p.ID,
                    Name = p.Name,
                    Category = p.Category.Name,
                    Price = p.Price,
                    Additives = p.PossibleAdditives.Select(a => new
                    {
                        ID = a.ID,
                        Name = a.Name,
                        Price = a.Price
                    })
                })                    
            });

If e.ProductsSelling is not null it will perform the linq query otherwise it will return null, this will prevent the NullReferenceException you are getting.

YuvShap
  • 3,825
  • 2
  • 10
  • 24
  • thanks. query now doesn't thow error, but result list is null... but before: Establishments are not empty, all fine with them. – Amelina Oct 07 '16 at 17:28
  • How do you know result list is null, what are you trying to do with it? do you get any error? – YuvShap Oct 07 '16 at 17:31
  • I'm trying to return it from server. and in return Json(result); I get null exception again. – Amelina Oct 07 '16 at 17:33
  • Maybe try to use the ToList() extension in the end of the query? LINQ use a deferred execution mechanism and I think this causes you the problem. – YuvShap Oct 07 '16 at 17:39
  • 1
    Can you put a break point after the query to assure it is null? I think your problem is not in the query itself but in the Json parsing part. – YuvShap Oct 07 '16 at 17:43
  • I commented strings with json - no errors, the I wrote foreach (var r in result) Log.Logger.Debug(r.ProductsSelling.ToList().Count.ToString()); and null exception've come back! – Amelina Oct 07 '16 at 19:07
  • Look at the exception message, find out which of the class propeties is null and then you can use '?' oprator before it too. I would guess it is the PossibleValues in the inner query but it might be Category or Company properties also. – YuvShap Oct 07 '16 at 19:14