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. is this good C# linq to sql code?

is this good C# linq to sql code?

Scheduled Pinned Locked Moved C#
csharpquestiondatabaselinqhelp
12 Posts 6 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.
  • M Offline
    M Offline
    Member 8217517
    wrote on last edited by
    #1

    I would like to know if the following code is written ok or is there a better way? If there is a better way, can you explain and/or point me to a reference I can use? Is the linq writing from one datacontext object to some data fields ok? Also is the try catch adequate? I do know the code works since I stepped through the code. I am asking the question is this is my first time working with linq to sql in a C# 2010 application and I am the only programmer at my company.

    protected void Ready_Data(Attes att, AtsDataContext attDataContext)
    {

            try {
    
          var results = from s in attDataContext.Attes\_Schedules
                          where s.Pay\_Month == att.Pay\_Month\_Date
                          select new
                          {
                              PayDate = s.Pay\_Month,
                              CurCStopDate = s.Current\_C\_Stop\_Date,
                              PriorCStopDate = s.Prior\_C\_Stop\_Date,
                              RptsReceviedMonth = s.Rpts\_Rec\_Month,
                                                         
                          };
    
            foreach (var r in results)
            {
                att.Pay\_Month\_Date = r.PayDate;
                att.Current\_C\_Stop\_Date = r.CurCStopDate;
                att.Prior\_C\_Stop\_Date = r.PriorCStopDate;
                att.C\_Rpts\_Rec\_Date = r.RptsReceviedMonth;
            }
           
            }
            catch (Exception ex)
            {
                lblErrorMsg.Text = "Attes Page Processing problem";
                lblErrorMsg.Visible = true;
                hold\_Exception = ex;
    
            }
    
        }
    
    N S O 3 Replies Last reply
    0
    • M Member 8217517

      I would like to know if the following code is written ok or is there a better way? If there is a better way, can you explain and/or point me to a reference I can use? Is the linq writing from one datacontext object to some data fields ok? Also is the try catch adequate? I do know the code works since I stepped through the code. I am asking the question is this is my first time working with linq to sql in a C# 2010 application and I am the only programmer at my company.

      protected void Ready_Data(Attes att, AtsDataContext attDataContext)
      {

              try {
      
            var results = from s in attDataContext.Attes\_Schedules
                            where s.Pay\_Month == att.Pay\_Month\_Date
                            select new
                            {
                                PayDate = s.Pay\_Month,
                                CurCStopDate = s.Current\_C\_Stop\_Date,
                                PriorCStopDate = s.Prior\_C\_Stop\_Date,
                                RptsReceviedMonth = s.Rpts\_Rec\_Month,
                                                           
                            };
      
              foreach (var r in results)
              {
                  att.Pay\_Month\_Date = r.PayDate;
                  att.Current\_C\_Stop\_Date = r.CurCStopDate;
                  att.Prior\_C\_Stop\_Date = r.PriorCStopDate;
                  att.C\_Rpts\_Rec\_Date = r.RptsReceviedMonth;
              }
             
              }
              catch (Exception ex)
              {
                  lblErrorMsg.Text = "Attes Page Processing problem";
                  lblErrorMsg.Visible = true;
                  hold\_Exception = ex;
      
              }
      
          }
      
      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      results is a collection you are integrating over yet att is a single object. That means if you have five items in results att will only be assigned the values in the last item when this method returns. If you only want/need a single item then use Single or SingleOrDefault method, or First or FirstOrDefault if that makes sense for you data


      No comment

      M 1 Reply Last reply
      0
      • M Member 8217517

        I would like to know if the following code is written ok or is there a better way? If there is a better way, can you explain and/or point me to a reference I can use? Is the linq writing from one datacontext object to some data fields ok? Also is the try catch adequate? I do know the code works since I stepped through the code. I am asking the question is this is my first time working with linq to sql in a C# 2010 application and I am the only programmer at my company.

        protected void Ready_Data(Attes att, AtsDataContext attDataContext)
        {

                try {
        
              var results = from s in attDataContext.Attes\_Schedules
                              where s.Pay\_Month == att.Pay\_Month\_Date
                              select new
                              {
                                  PayDate = s.Pay\_Month,
                                  CurCStopDate = s.Current\_C\_Stop\_Date,
                                  PriorCStopDate = s.Prior\_C\_Stop\_Date,
                                  RptsReceviedMonth = s.Rpts\_Rec\_Month,
                                                             
                              };
        
                foreach (var r in results)
                {
                    att.Pay\_Month\_Date = r.PayDate;
                    att.Current\_C\_Stop\_Date = r.CurCStopDate;
                    att.Prior\_C\_Stop\_Date = r.PriorCStopDate;
                    att.C\_Rpts\_Rec\_Date = r.RptsReceviedMonth;
                }
               
                }
                catch (Exception ex)
                {
                    lblErrorMsg.Text = "Attes Page Processing problem";
                    lblErrorMsg.Visible = true;
                    hold\_Exception = ex;
        
                }
        
            }
        
        S Offline
        S Offline
        SledgeHammer01
        wrote on last edited by
        #3

        Just an FYI... you should not be writing LINQ2SQL code going forward as it has been COMPLETELY abandoned by Microsoft. They are now strictly on Entity Framework.

        Richard Andrew x64R N B 3 Replies Last reply
        0
        • S SledgeHammer01

          Just an FYI... you should not be writing LINQ2SQL code going forward as it has been COMPLETELY abandoned by Microsoft. They are now strictly on Entity Framework.

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          That's cool. Now I don't have to bother learning it! :rolleyes:

          The difficult we do right away... ...the impossible takes slightly longer.

          1 Reply Last reply
          0
          • M Member 8217517

            I would like to know if the following code is written ok or is there a better way? If there is a better way, can you explain and/or point me to a reference I can use? Is the linq writing from one datacontext object to some data fields ok? Also is the try catch adequate? I do know the code works since I stepped through the code. I am asking the question is this is my first time working with linq to sql in a C# 2010 application and I am the only programmer at my company.

            protected void Ready_Data(Attes att, AtsDataContext attDataContext)
            {

                    try {
            
                  var results = from s in attDataContext.Attes\_Schedules
                                  where s.Pay\_Month == att.Pay\_Month\_Date
                                  select new
                                  {
                                      PayDate = s.Pay\_Month,
                                      CurCStopDate = s.Current\_C\_Stop\_Date,
                                      PriorCStopDate = s.Prior\_C\_Stop\_Date,
                                      RptsReceviedMonth = s.Rpts\_Rec\_Month,
                                                                 
                                  };
            
                    foreach (var r in results)
                    {
                        att.Pay\_Month\_Date = r.PayDate;
                        att.Current\_C\_Stop\_Date = r.CurCStopDate;
                        att.Prior\_C\_Stop\_Date = r.PriorCStopDate;
                        att.C\_Rpts\_Rec\_Date = r.RptsReceviedMonth;
                    }
                   
                    }
                    catch (Exception ex)
                    {
                        lblErrorMsg.Text = "Attes Page Processing problem";
                        lblErrorMsg.Visible = true;
                        hold\_Exception = ex;
            
                    }
            
                }
            
            O Offline
            O Offline
            omidh2007
            wrote on last edited by
            #5

            That,s good . but you can use Entity FrameWork and Lambda Expression instead . Anyway there are useful LINQ Samples here ==> http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b[^]

            1 Reply Last reply
            0
            • N Not Active

              results is a collection you are integrating over yet att is a single object. That means if you have five items in results att will only be assigned the values in the last item when this method returns. If you only want/need a single item then use Single or SingleOrDefault method, or First or FirstOrDefault if that makes sense for you data


              No comment

              M Offline
              M Offline
              Member 8217517
              wrote on last edited by
              #6

              In this code I wanted to retreive the 5 items. I think this code returns all 5 items.

              N 1 Reply Last reply
              0
              • S SledgeHammer01

                Just an FYI... you should not be writing LINQ2SQL code going forward as it has been COMPLETELY abandoned by Microsoft. They are now strictly on Entity Framework.

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

                Not quite correct Linq2SQL has not been abondoned, there is more effort and emphasis being placed on EF now and Linq2SQL is not getting as much work. It was the same argument between WinForms and WPF. WinForms has not been abandoned http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/75f96c32-25c2-4bf8-9693-d418f12e6f92/[^]


                No comment

                S 1 Reply Last reply
                0
                • M Member 8217517

                  In this code I wanted to retreive the 5 items. I think this code returns all 5 items.

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

                  No it doesn't. You said you have stepped through the code. Didn't you notice the att object getting overwritten with new values each time? att is not a collection it is a single instance. Unless you have mistyped or not given complete information.


                  No comment

                  1 Reply Last reply
                  0
                  • N Not Active

                    Not quite correct Linq2SQL has not been abondoned, there is more effort and emphasis being placed on EF now and Linq2SQL is not getting as much work. It was the same argument between WinForms and WPF. WinForms has not been abandoned http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/75f96c32-25c2-4bf8-9693-d418f12e6f92/[^]


                    No comment

                    S Offline
                    S Offline
                    SledgeHammer01
                    wrote on last edited by
                    #9

                    The link you included says pretty much what I said! :confused: :confused: :confused: Linq2SQL is not getting any new features and is in "maintainence mode". That means *all* new dev effort is going into EF. I never said Microsoft was REMOVING Linq2SQL, I just said they abandoned it. Your very own Linq (no pun intended :-D ) makes it pretty clear that Linq2SQL is pretty much "meh... if we find a serious bug in it thats a show stopper, we MAY fix it". But whatever :). All I was telling the OP was that Microsoft isn't going to be doing anything with it going forward, so any new features are going to be strictly EF, so he shouldn't waste his time building his app around it.

                    N 1 Reply Last reply
                    0
                    • S SledgeHammer01

                      The link you included says pretty much what I said! :confused: :confused: :confused: Linq2SQL is not getting any new features and is in "maintainence mode". That means *all* new dev effort is going into EF. I never said Microsoft was REMOVING Linq2SQL, I just said they abandoned it. Your very own Linq (no pun intended :-D ) makes it pretty clear that Linq2SQL is pretty much "meh... if we find a serious bug in it thats a show stopper, we MAY fix it". But whatever :). All I was telling the OP was that Microsoft isn't going to be doing anything with it going forward, so any new features are going to be strictly EF, so he shouldn't waste his time building his app around it.

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

                      There is quite difference between abandoned and maintenance. Abandoned means no further support which is not the case.


                      No comment

                      S 1 Reply Last reply
                      0
                      • N Not Active

                        There is quite difference between abandoned and maintenance. Abandoned means no further support which is not the case.


                        No comment

                        S Offline
                        S Offline
                        SledgeHammer01
                        wrote on last edited by
                        #11

                        I think you are just arguing semantics at this point. If Microsoft tells you "we aren't going to be developing this any further", it is your choice to ignore them and continue down a dead end path.

                        1 Reply Last reply
                        0
                        • S SledgeHammer01

                          Just an FYI... you should not be writing LINQ2SQL code going forward as it has been COMPLETELY abandoned by Microsoft. They are now strictly on Entity Framework.

                          B Offline
                          B Offline
                          BobJanova
                          wrote on last edited by
                          #12

                          Microsoft aren't adding extensions to + and -, either. That doesn't mean you shouldn't use them. Linq to SQL is part of the language and the Framework, and will remain so, and it's much nicer to use in certain circumstances than the equivalent code with DbAdapters and DataSets. It's absolutely fine to use it even if it's not getting extended in future (which is a big step away from 'completely abandoned'). EF is massive overkill for a lot of applications, particularly if you already have a database and can't use its magic wand to match everything up.

                          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