开发者

Need Linq Query Help

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 se开发者_如何转开发rial 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜