none
Is this good linq to sql code to update table? RRS feed

  • Question

  • 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;

                }

            }





    Friday, November 11, 2011 8:38 PM

Answers

  • Hi midnight_car,

    please modify your linq statement like this:

    var reasult = (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,
                          }).First();

    Then you can call result.PayDate, result.CurCStopDate and so on.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by midnight_car Tuesday, November 15, 2011 12:33 PM
    Tuesday, November 15, 2011 2:05 AM
    Moderator

All replies

  • Hi midnight_car,

    After researching your code, I found a little problem. In the foreach statement, every time you assign a new value to the property, then the result will always be the last instance of your select result, it may not what you want. When you define a query expression, it will not really query data from database, it only execute when the foreach statement is run, so, put a try statement before the query expression seems useless. More about the LINQ to SQL’s execute mechanism, you can refer here.

     

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, November 14, 2011 7:13 AM
    Moderator
  • I am running this setup prior to running  an 'update' for one row to the database.

    I would like to know how to change the code so NO foreach loop is required.

     Basically how can I do the following code with NO  foreach statment:

    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;

     

    Thanks!



    Monday, November 14, 2011 3:04 PM
  • Hi midnight_car,

    please modify your linq statement like this:

    var reasult = (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,
                          }).First();

    Then you can call result.PayDate, result.CurCStopDate and so on.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by midnight_car Tuesday, November 15, 2011 12:33 PM
    Tuesday, November 15, 2011 2:05 AM
    Moderator