is this good C# linq to sql code?
-
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; } }
-
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; } }
results
is a collection you are integrating over yetatt
is a single object. That means if you have five items inresults
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
-
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; } }
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.
-
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.
That's cool. Now I don't have to bother learning it! :rolleyes:
The difficult we do right away... ...the impossible takes slightly longer.
-
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; } }
-
results
is a collection you are integrating over yetatt
is a single object. That means if you have five items inresults
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
In this code I wanted to retreive the 5 items. I think this code returns all 5 items.
-
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.
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
-
In this code I wanted to retreive the 5 items. I think this code returns all 5 items.
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
-
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
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.
-
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.
There is quite difference between abandoned and maintenance. Abandoned means no further support which is not the case.
No comment
-
There is quite difference between abandoned and maintenance. Abandoned means no further support which is not the case.
No comment
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.
-
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.
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.