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. C#
  4. Linq SQL Help

Linq SQL Help

Scheduled Pinned Locked Moved C#
databasequestioncsharplinqhelp
7 Posts 3 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.
  • G Offline
    G Offline
    gmhanna
    wrote on last edited by
    #1

    Hi, I am new to Linq and I'm trying to access the MfgNumber after the query directly without a foreach loop. I don't need the foreach loop as the SQL query should be returning one result.

    [Table(Name = "Mfg")]
    public class Mfg
    {
    [Column(IsPrimaryKey = true)]
    public string MfgName;
    [Column]
    public string MfgNumber;
    }
    ...
    var q2 =
    from mfg in db.Mfg
    where mfg.MfgName == Common.productPublisherName
    select mfg;

       foreach (var mfg in q2)
           MfgNumber = mfg.MfgNumber; 
    

    How do I access the variables from the query without a foreach? Thank you,

    Glenn

    R N 2 Replies Last reply
    0
    • G gmhanna

      Hi, I am new to Linq and I'm trying to access the MfgNumber after the query directly without a foreach loop. I don't need the foreach loop as the SQL query should be returning one result.

      [Table(Name = "Mfg")]
      public class Mfg
      {
      [Column(IsPrimaryKey = true)]
      public string MfgName;
      [Column]
      public string MfgNumber;
      }
      ...
      var q2 =
      from mfg in db.Mfg
      where mfg.MfgName == Common.productPublisherName
      select mfg;

         foreach (var mfg in q2)
             MfgNumber = mfg.MfgNumber; 
      

      How do I access the variables from the query without a foreach? Thank you,

      Glenn

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      This should meet your needs:

      var q2 = from mfg in db.Mfg
      where mfg.MfgName == Common.productPublisherName
      select mfg.MfgNumber;

      foreach (var string mfgNumber in q2) {
      ....
      }

      I just re-read your post and realized you expect there to be (at most) one entry, so Mark's answer is the one you want. /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      modified on Sunday, August 7, 2011 10:52 AM

      N 1 Reply Last reply
      0
      • G gmhanna

        Hi, I am new to Linq and I'm trying to access the MfgNumber after the query directly without a foreach loop. I don't need the foreach loop as the SQL query should be returning one result.

        [Table(Name = "Mfg")]
        public class Mfg
        {
        [Column(IsPrimaryKey = true)]
        public string MfgName;
        [Column]
        public string MfgNumber;
        }
        ...
        var q2 =
        from mfg in db.Mfg
        where mfg.MfgName == Common.productPublisherName
        select mfg;

           foreach (var mfg in q2)
               MfgNumber = mfg.MfgNumber; 
        

        How do I access the variables from the query without a foreach? Thank you,

        Glenn

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

        Use Single if the query will return a single result. q2 will be of type Mfg

        var q2 =(from mfg in db.Mfg
        where mfg.MfgName == Common.productPublisherName
        select mfg).Single();

        or in case the query returns nothing create an object with default values, or null.

        var q2 =(from mfg in db.Mfg
        where mfg.MfgName == Common.productPublisherName
        select mfg).SingleOrDefault();


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

        1 Reply Last reply
        0
        • R Ravi Bhavnani

          This should meet your needs:

          var q2 = from mfg in db.Mfg
          where mfg.MfgName == Common.productPublisherName
          select mfg.MfgNumber;

          foreach (var string mfgNumber in q2) {
          ....
          }

          I just re-read your post and realized you expect there to be (at most) one entry, so Mark's answer is the one you want. /ravi

          My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

          modified on Sunday, August 7, 2011 10:52 AM

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

          Ravi Bhavnani wrote:

          foreach (var string mfgNumber in q2) {

          The OP did say without for loop :rolleyes:


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

          R 1 Reply Last reply
          0
          • N Not Active

            Ravi Bhavnani wrote:

            foreach (var string mfgNumber in q2) {

            The OP did say without for loop :rolleyes:


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

            R Offline
            R Offline
            Ravi Bhavnani
            wrote on last edited by
            #5

            Mark Nischalke wrote:

            The OP did say without for loop

            The loop in my response processes the returned data - it doesn't extract it. /ravi

            My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

            N 1 Reply Last reply
            0
            • R Ravi Bhavnani

              Mark Nischalke wrote:

              The OP did say without for loop

              The loop in my response processes the returned data - it doesn't extract it. /ravi

              My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

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

              The only thing your code does different from the OPs is return a single property of the object rather than the object as a whole. LINQ queries are not executed until requested such as when the GetEnumerator method is called in a for each statement. So, yes your statement does extract the data, then processes it.


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

              R 1 Reply Last reply
              0
              • N Not Active

                The only thing your code does different from the OPs is return a single property of the object rather than the object as a whole. LINQ queries are not executed until requested such as when the GetEnumerator method is called in a for each statement. So, yes your statement does extract the data, then processes it.


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

                R Offline
                R Offline
                Ravi Bhavnani
                wrote on last edited by
                #7

                I just re-read the original post and realized he expects only one entry. :doh:  My bad. /ravi

                My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                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