我在使用 LINQ(C# 语言)时遇到问题。我需要按字段排序记录列表,应该是int
,但有时不是:
from MyObject obj in new MyObject()
where obj.Visibile=="1"
orderby Int32.Parse(obj.Order) ascending
select obj;
而且,正如我所说,如果 obj.Order 不是int
,我收到错误:System.FormatException:输入字符串的格式不正确。
我想将非 int 项放在列表末尾,不会出现任何错误。是否可以?
尝试使用 TryParse
int myInt;
from obj in MYobjects
where obj.Visibile=="1"
orderby (int.TryParse(Str, out myInt) ? myInt: 0 )
select obj;
OR
MYobjects.OrderBy(r => Number(r.str);
//private function
int Number(string str)
{
int result_ignored;
if (int.TryParse(str,out result_ignored))
return result_ignored;
else
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)