|
-
Nov 18th, 2012, 08:56 PM
#1
Thread Starter
New Member
VS2012 Linq to SQL retrieving highest ID number
Hi everyone, I'm working with the infamous Northwind database w/in SQL 2008. I'm working on the basic Insert, Update, Delete functions by way of LINQ to SQL. I currently have things working pretty well but I'm having issues with a table that isn't auto-incremented for its ID number.
I'm looking for the equivalent of the C# (I believe) .max method so I can search the table for the highest ID number and increment + 1. The count method isn't working
because if I use that and delete rows and its ID number is the same as the .count, then I will get a unique PK exception thrown.
Any help would be awesome, I've been going at this and search all day to no avail.
This is my Insert Class thus far
Code:
Public Class InsertClass
Sub insertsub(ByVal regiondescription As String)
Dim insert As New RegionLinqSQLDataContext
Dim newregion As New Region With _
{.RegionDescription = frmInsert.txtboxRegionDesc.Text, _
.RegionID = insert.Regions.Count + 2}
insert.Regions.InsertOnSubmit(newregion)
Try
insert.SubmitChanges()
DisplayRegions.Show()
MessageBox.Show("Insert successful")
Catch ex As Exception
MessageBox.Show("Insert not successful")
End Try
End Sub
End Class
-
Nov 18th, 2012, 09:46 PM
#2
Re: VS2012 Linq to SQL retrieving highest ID number
Max is not a C# method. It's a .NET method. Just as you use the same Form.Show method to display a form in both C# and VB, so you use the same Enumerable.Max method to get the maximum value from a sequence in both C# and VB. The difference between the two languages is in the syntax of the lambda expression you pass to the Max method. While a C# lambda might look like this:the equivalent VB lambda would look like this:
Code:
Function(r) r.RegionID
-
Nov 18th, 2012, 09:56 PM
#3
Thread Starter
New Member
Re: VS2012 Linq to SQL retrieving highest ID number
So instead of
Code:
RegionID = insert.Regions.Count + 2
I'd insert
Code:
regionID = max(r) r.RegionID
?
I'm not exactly getting what you're saying.
Thanks for the reply BTW.
-
Nov 18th, 2012, 10:03 PM
#4
Re: VS2012 Linq to SQL retrieving highest ID number
Max is a method that you call on the sequence you want to get the maximum value from and you pass it a lambda expression as an argument. If you've seen it done in C#, which presumably you have if you mention it in relation to C#, then it's called in exactly the same way. As I said, the only different is the syntax of the lambda argument.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|