個人檔案Nates Stuff相片部落格清單更多 ![]() | 說明 |
|
|
12月10日 Visual Studio 2008 RTM Changes to LINQ to SQLFor those who haven't heard Visual Studio 2008 has officially been released! That also means that the .net Framework 3.5 has also been released! For those who have done the Beta's there is a couple of method renaming that has happened that will break your bits! Summary
I think I can understand why they made the change. When you had a line that looked like "dbContext.Documents.Add( mydoc );" it never really felt like I was inserting a record into my database (in fact you are not inserting, not yet! Not until you call .SubmitChanges(); on the DataContext. The same goes for the delete. It was never clear if LINQ was going to "notice" the new record and make sure it is properly added to the database. So here are are some Linq Samples that we all love!
db.Documents.InsertOnSubmit( d );
// Delete a record (or multiple) var doc = from d in db.Documents
// Select One and Select Many
var OldDocs = from doc in db.Documents where doc.ModifiedDate > DateTime.Now.AddMonths(-12) select doc; foreach (Document doc3 in OldDocs) { doc3.Archived = true; } // Many ToList :: Select many and return to a typed list now var NewDocs = from doc in db.Documents where doc.CreatedDate > DateTime.Now.AddMonths(-1) select doc; List<Document> newDocList = NewDocs.ToList(); // Many Custom Type :: Select many documents but return specific type List<Notifications> changeNotifications = (from doc in db.Documents where doc.CreatedDate > DateTime.Now.AddMonths(-1) || doc.ModifiedDate > DateTime.Now.AddMonths(-2) select new Notification() { NotificationType = ChangeNotificationTypeEnum.All, NotificationDate = doc.ModifiedDate ?? doc.CreatedDate, Text = doc.Title }).ToList(); |
|
|