将字符串转换为foreach loop c#剃刀页
我正在使用foreach
循环从数据库中检索数据,并且包括带有文本和数字的字段。我需要将带有数字从字符串的字段转换为int,以便能够正确对数字字段进行正确排序。
我从另一个线程尝试了以下内容,但对我不起作用:
// Displays the value as string
Convert.ToInt32(item.property);
// Throws an exception error "The best overloaded method match for 'int.TryParse(string out int' has some invalid arguments)"
Int32.TryParse(item.property, out PropertyInt);
以下是:
但这在foreach循环中对我不起作用。
这是我的代码:
@foreach (var item in db.Query(collection))
{
<tr>
<td>item.name</td>
<td>@Convert.ToInt32(item.value);</td>
<td>Int32.TryParse(item.value, out ItemValueInt);</td>
</tr>
}
I am using a foreach
loop to retrieve data from database and it includes both fields with text and numbers. I need to convert the fields with numbers from string to int to be able to sort the number fields properly.
I tried the following from another thread but it is not working for me:
// Displays the value as string
Convert.ToInt32(item.property);
// Throws an exception error "The best overloaded method match for 'int.TryParse(string out int' has some invalid arguments)"
Int32.TryParse(item.property, out PropertyInt);
Followed following:
How can I convert String to Int?
But this is not working for me in a foreach loop.
Here is my code:
@foreach (var item in db.Query(collection))
{
<tr>
<td>item.name</td>
<td>@Convert.ToInt32(item.value);</td>
<td>Int32.TryParse(item.value, out ItemValueInt);</td>
</tr>
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用TryParse检查字符串是否可以转换为INT,如果为true,则显示
itemValueInt
,这是一个演示:Try to use TryParse to check if the string can be converted to int,and if true, show the
ItemValueInt
,here is a demo: