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 get single value from linq query without using loop

How to get single value from linq query without using loop

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqhelptutorial
6 Posts 4 Posters 12 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.
  • M Offline
    M Offline
    meeram395
    wrote on last edited by
    #1

    I am using a linq query in a datatable for retrieving a single string value. Since it is a single value, I am looking into an option whether it is possible to retrieve the value without using foreach loop. Following is my code

    string ejvqry = string.Empty;
    var query = from frstqry in dsquery.Tables[0].AsEnumerable() select new { frstqrystring = frstqry.Field<string>("appendqry") };

    foreach (var fixedqry in query)
    {
    ejvqry = fixedqry.frstqrystring;
    }

    Is there any way which I can get the value without foreach loop? I am sure that there will be only one value always. I tried to use Take, TakeWhile etc. But it is retrieiving some unknown characters. Please help. Thanks in advance.

    Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

    P M 2 Replies Last reply
    0
    • M meeram395

      I am using a linq query in a datatable for retrieving a single string value. Since it is a single value, I am looking into an option whether it is possible to retrieve the value without using foreach loop. Following is my code

      string ejvqry = string.Empty;
      var query = from frstqry in dsquery.Tables[0].AsEnumerable() select new { frstqrystring = frstqry.Field<string>("appendqry") };

      foreach (var fixedqry in query)
      {
      ejvqry = fixedqry.frstqrystring;
      }

      Is there any way which I can get the value without foreach loop? I am sure that there will be only one value always. I tried to use Take, TakeWhile etc. But it is retrieiving some unknown characters. Please help. Thanks in advance.

      Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      I tend to use the .SingleOrDefault(); for this.

      I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      Z 1 Reply Last reply
      0
      • M meeram395

        I am using a linq query in a datatable for retrieving a single string value. Since it is a single value, I am looking into an option whether it is possible to retrieve the value without using foreach loop. Following is my code

        string ejvqry = string.Empty;
        var query = from frstqry in dsquery.Tables[0].AsEnumerable() select new { frstqrystring = frstqry.Field<string>("appendqry") };

        foreach (var fixedqry in query)
        {
        ejvqry = fixedqry.frstqrystring;
        }

        Is there any way which I can get the value without foreach loop? I am sure that there will be only one value always. I tried to use Take, TakeWhile etc. But it is retrieiving some unknown characters. Please help. Thanks in advance.

        Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

        M Offline
        M Offline
        MasttsaM
        wrote on last edited by
        #3

        inject the "break;" into your foreach statement string ejvqry = string.Empty; var query = from frstqry in dsquery.Tables[0].AsEnumerable() select new { frstqrystring = frstqry.Field<string>("appendqry") }; foreach (var fixedqry in query){ ejvqry = fixedqry.frstqrystring; break; } Reference: http://www.programlive.tk have some C# simple example code here

        P 1 Reply Last reply
        0
        • M MasttsaM

          inject the "break;" into your foreach statement string ejvqry = string.Empty; var query = from frstqry in dsquery.Tables[0].AsEnumerable() select new { frstqrystring = frstqry.Field<string>("appendqry") }; foreach (var fixedqry in query){ ejvqry = fixedqry.frstqrystring; break; } Reference: http://www.programlive.tk have some C# simple example code here

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          I have to ask: why? There's a perfectly suitable mechanism in place in Linq to support this - why not use the available tools to achieve this?

          I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • P Pete OHanlon

            I tend to use the .SingleOrDefault(); for this.

            I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            Z Offline
            Z Offline
            zommarin
            wrote on last edited by
            #5

            It might be good to note that .Single() is more strict, it requires that you have exactly one value. .SingleOrDefault(); is good when you want a null when nothis is found and .Take(1) is useful when you just want the first one no matter what.

            P 1 Reply Last reply
            0
            • Z zommarin

              It might be good to note that .Single() is more strict, it requires that you have exactly one value. .SingleOrDefault(); is good when you want a null when nothis is found and .Take(1) is useful when you just want the first one no matter what.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              zommarin wrote:

              .SingleOrDefault(); is good when you want a null when nothis is found

              Not quite. If no element is found, and it's a none nullable type then the default is returned, e.g. an int would return 0.

              I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              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