none
Null reference handling in LINQ IEnumerable RRS feed

  • Question

  • So. I have what I hope to be a quick question. How do you handle nulls with IEnumerable<T> data sets?

    When using IQueryable it works as you would be expected. If you traverse to a related table in a select statement and you come across a  null object, no exception is trhown, you just end up getting null back in your select.

    If I choose to hold some data in an in memory cache, storing them as IEnumerable<T>. Some of the properties I will be pulling out can be quite nested. Say for example.


    GridView.DataSource = CachedList.Where(p => p.RLProductID = 11)
    .Select( pr => new {
    pr.RLProductAuthor.RLAuthor.FirstName,
    pr.RLProductAuthor.RLAuthor.LastName,
    pr.RLProduct.ProductName
    });

    My hope would be to get null for FirstName and LastName if the RLProductAuthor, or RLAuthor end up being null. Instead I get a null reference exception.

    How would I make this work given that sometimes some of these values might be null? Would I have to make long chains of ?? operations to get to the final object that I am trying to capture?

    Any help would be appreciated. I have been looking and can not find an elegant solution.

    Sunday, January 9, 2011 3:02 AM

Answers

  • I got that wrong. It would be like this:

    FirstName = pr.RLProductAuthor == null ? (object) null : (pr.RLProductAuthor.RLAuthor == null ? (object) null : pr.RLProductAuthor.RLAuthor.FirstName)

    Monday, January 10, 2011 11:39 PM

All replies

  •  

    Hi simplybrianp,

    Thanks for your post.

    According to your description, you want to handle the null reference exception when RLProductAuthor or RLAuthor is null, if I misunderstand you, please clarify, thanks for understanding.

    I saw your code, you want to show FirstName and LastName on GridView, I guess you want to show "Null" string if the Author is null, am I right?

    If so, I think "? :" Operation is good way to handle this situation, you may write like this way:

    GridView.DataSource = CachedList.Where(p => p.RLProductID = 11)

    .Select( pr => new {

    FirstName=pr.RLProductAuthor.RLAuthor==null?"NULL":pr.RLProductAuthor.RLAuthor.FirstName

    });

     

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 10, 2011 6:54 AM
    Moderator
  • I could do that. What I actually want would be to replicate the null handling that IQueryable seems to have. In this example I would really have to do this then:

    I would actually need to check for null on RLProductAuthor as well, since RLProductAuthor.RLAuthor will give a null reference exception if it is null. Thus the expression gets really complex:

    GridView.DataSource = CachedList.Where(p => p.RLProductID = 11)

    .Select( pr => new {

    FirstName = pr.RLProductAuthor == null ? (object) null : (pr.RLProductAuthor.RLAuthor == (object) null ? null : pr.RLProductAuthor.RLAuthor.FirstName)

    });

     

    With this solution it would seem that expressions with more than a 2 relationships to traverse would become really difficult to read. I really enjoy the ability of IQueryable to handle these nulls, and was hoping that I could move to an in memory representation of the data structure with the same elegance.

    Monday, January 10, 2011 7:47 PM
  • I got that wrong. It would be like this:

    FirstName = pr.RLProductAuthor == null ? (object) null : (pr.RLProductAuthor.RLAuthor == null ? (object) null : pr.RLProductAuthor.RLAuthor.FirstName)

    Monday, January 10, 2011 11:39 PM
  • Hi simplybrianp,

    Yeah, you are right. it is a complex logic.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 11, 2011 2:58 AM
    Moderator