views:

19

answers:

1

I have the following simple Linq query:

(from container in Container
join containerType in ContainerType on container.ContainerType equals containerType
where containerType.ContainerTypeID == 2
select container).Max (row => row.SerialNumber)

As is, this query works fine. The problem is that SerialNumber, in the DB, is an nvarchar type. When ContainerTypeID = 2, the values in this column will always be integers, but not zero-filled. Therefore, doing a Max, without casting all the serial numbers to integers, won't work (e.g., Max would select '2' over '10'). So, my question is, how can I cast all the values of row.SerialNumber to an integer so Max can find the greatest serial number?

A: 

I tried to cast this way and it worked.

(from container in Container
join containerType in ContainerType on container.ContainerType equals containerType
where containerType.ContainerTypeID == 2
select container).Max (row => Convert.ToInt32(row.SerialNumber))

But if the value in row.SerialNumber is greater than int or an invalid value cause an exception.

Krunal
@Krunal - Thanks, that worked! I was afraid I couldn't cast like that because I tried using Parse and got an error.
Randy Minder