Dealing with NULL collection in Linq query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Deckarep

    Dealing with NULL collection in Linq query


    Hey everyone,

    Is there a more elegant or cleaner way of accomplishing the following
    null check?


    List<stringmySt ring = null; //Purposely null list of strings to
    show the example

    XElement element = new XElement("Strin gs",
    (myString != null) ?
    (from string s in myString
    where s.StartsWith("S ")
    select s) : null
    );

    This XElement yields the following result: <Strings/and empty
    Element.


    I like that I can write linq to xml in nested declarations like this.
    What I don't like now...is doing the null
    check with the tertiary starts making the code ugly and less
    readable. I know how to handle null values
    when some of the contents in the collection may be null but what if
    your collection is null itself??

    If there's a better more Linq integrated way of handling this that
    would be great.

    Thanks,
  • Jeroen Mostert

    #2
    Re: Dealing with NULL collection in Linq query

    Deckarep wrote:
    Is there a more elegant or cleaner way of accomplishing the following
    null check?
    >
    >
    List<stringmySt ring = null; //Purposely null list of strings to
    show the example
    >
    XElement element = new XElement("Strin gs",
    (myString != null) ?
    (from string s in myString
    where s.StartsWith("S ")
    select s) : null
    );
    >
    This XElement yields the following result: <Strings/and empty
    Element.
    >
    >
    I like that I can write linq to xml in nested declarations like this.
    What I don't like now...is doing the null
    check with the tertiary starts making the code ugly and less
    readable. I know how to handle null values
    when some of the contents in the collection may be null but what if
    your collection is null itself??
    >
    If there's a better more Linq integrated way of handling this that
    would be great.
    >
    Don't use null references for collections, use empty collections instead:

    List<stringmySt ring = new List<string>();

    Edge case disappears. Also, a lambda is clearer here:

    XElement element = new XElement(
    "Strings",
    myString.Where( s =s.StartsWith(" S"))
    );

    --
    J.

    Comment

    • Gilles Kohl [MVP]

      #3
      Re: Dealing with NULL collection in Linq query

      On Thu, 15 May 2008 10:44:57 -0700 (PDT), Deckarep
      <deckarep@gmail .comwrote:
      >
      >Hey everyone,
      >
      >Is there a more elegant or cleaner way of accomplishing the following
      >null check?
      >
      >
      >List<stringmyS tring = null; //Purposely null list of strings to
      >show the example
      >
      >XElement element = new XElement("Strin gs",
      (myString != null) ?
      (from string s in myString
      where s.StartsWith("S ")
      select s) : null
      );
      >
      >This XElement yields the following result: <Strings/and empty
      >Element.
      >
      >
      >I like that I can write linq to xml in nested declarations like this.
      >What I don't like now...is doing the null
      >check with the tertiary starts making the code ugly and less
      >readable. I know how to handle null values
      >when some of the contents in the collection may be null but what if
      >your collection is null itself??
      >
      >If there's a better more Linq integrated way of handling this that
      >would be great.
      Hmm, do you need to be able to differentiate between a null myString
      and an empty list of strings?

      That is, could you use List<stringmySt ring = new List<string>();
      and get rid of the tertiary?

      Alternatively, something along these lines:

      XElement element = new XElement("Strin gs",
      (myString ?? new List<string>()) .Where(s =s.StartsWith(" S")));

      Neither strike me as a lot cleaner or more elegant though :-)

      Regards,
      Gilles.

      Comment

      • Deckarep

        #4
        Re: Dealing with NULL collection in Linq query

        Good point. I would use an empty collection if I could get away with
        it. Unfortunately I'm using a system
        that was not designed in this fashion so I often have to check for
        null before knowing
        if a collection is empty or not. :(

        Thanks for the response tip on Lambda!

        Comment

        • Jeroen Mostert

          #5
          Re: Dealing with NULL collection in Linq query

          Deckarep wrote:
          Good point. I would use an empty collection if I could get away with
          it. Unfortunately I'm using a system
          that was not designed in this fashion so I often have to check for
          null before knowing
          if a collection is empty or not. :(
          >
          If you really need to do this a lot, you can cheat your way around it:

          public static class EnumerableExten sions {
          public static IEnumerable<TEm ptyIfNull<T>(th is IEnumerable<T>
          source) {
          return source ?? Enumerable.Empt y<T>();
          }
          }

          Now

          List<stringstri ngs = null;
          strings.EmptyIf Null().Where(s =s.StartsWith(" S"));

          will work.

          Disclaimer: while neat, I have no idea if this is inefficient compared to
          null checks and if so, exactly how much. Also, calling a method on a
          reference that's notionally null is obviously a little counterintuitiv e, so
          many people would just call this another abuse of extension methods. :-)

          --
          J.

          Comment

          Working...