Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. How To Test If Linq Query Returned Results

How To Test If Linq Query Returned Results

Scheduled Pinned Locked Moved LINQ
questioncsharpdatabaselinqtutorial
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have this:

    var query = from g in root.Groups
    from r in g.Rules
    from c in r.Conditions
    from a in c.Actions
    where g.Rules != null
    where r.Conditions != null
    where c.Actions != null
    where a.ActionID == ActionId
    select a;

    List<RuleAction> items = query.ToList();

    It fails on the last line with "Object reference not set to an instance of an object" because the query returned no results. How do I test for this before attempting to convert the results to a list? Thanks

    Everything makes sense in someone's mind

    N C U 3 Replies Last reply
    0
    • K Kevin Marois

      I have this:

      var query = from g in root.Groups
      from r in g.Rules
      from c in r.Conditions
      from a in c.Actions
      where g.Rules != null
      where r.Conditions != null
      where c.Actions != null
      where a.ActionID == ActionId
      select a;

      List<RuleAction> items = query.ToList();

      It fails on the last line with "Object reference not set to an instance of an object" because the query returned no results. How do I test for this before attempting to convert the results to a list? Thanks

      Everything makes sense in someone's mind

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      I believe you have other problems. Linq is smart enough to know to create an empty list when the query doesn't return any results.


      I know the language. I've read a book. - _Madmatt

      1 Reply Last reply
      0
      • K Kevin Marois

        I have this:

        var query = from g in root.Groups
        from r in g.Rules
        from c in r.Conditions
        from a in c.Actions
        where g.Rules != null
        where r.Conditions != null
        where c.Actions != null
        where a.ActionID == ActionId
        select a;

        List<RuleAction> items = query.ToList();

        It fails on the last line with "Object reference not set to an instance of an object" because the query returned no results. How do I test for this before attempting to convert the results to a list? Thanks

        Everything makes sense in someone's mind

        C Offline
        C Offline
        Chris Trelawny Ross
        wrote on last edited by
        #3

        The exception is thrown by one of the from clauses - because the where clauses are all executed after all the from clauses, so fail to excluded parent classes that have null collections. Try this instead:

        var query = from g in root.Groups where g.Rules != null
        	from r in g.Rules where r.Conditions != null
        	from c in r.Conditions where c.Actions != null
        	from a in c.Actions where a.ActionID == ActionId
        	select a;
        
        1 Reply Last reply
        0
        • K Kevin Marois

          I have this:

          var query = from g in root.Groups
          from r in g.Rules
          from c in r.Conditions
          from a in c.Actions
          where g.Rules != null
          where r.Conditions != null
          where c.Actions != null
          where a.ActionID == ActionId
          select a;

          List<RuleAction> items = query.ToList();

          It fails on the last line with "Object reference not set to an instance of an object" because the query returned no results. How do I test for this before attempting to convert the results to a list? Thanks

          Everything makes sense in someone's mind

          U Offline
          U Offline
          User 2689127
          wrote on last edited by
          #4

          Just reading this and thought I would jump in. I typically use query.FirstorDefault to verify it. You could also use query.Count>0 However, I tried some sample code and got that message when I hadn't instantiated my datacontext properly. That may not be it but just something to verify.

          G 1 Reply Last reply
          0
          • U User 2689127

            Just reading this and thought I would jump in. I typically use query.FirstorDefault to verify it. You could also use query.Count>0 However, I tried some sample code and got that message when I hadn't instantiated my datacontext properly. That may not be it but just something to verify.

            G Offline
            G Offline
            Gideon Engelberth
            wrote on last edited by
            #5

            If all you are doing is checking for any result, using query.Any() would be better than query.Count() since Any will stop after one item instead of always iterating the whole sequence.

            U 1 Reply Last reply
            0
            • G Gideon Engelberth

              If all you are doing is checking for any result, using query.Any() would be better than query.Count() since Any will stop after one item instead of always iterating the whole sequence.

              U Offline
              U Offline
              User 2689127
              wrote on last edited by
              #6

              Thanks Gideon - I hadn't used Any for that purpose. Awesome tip!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups