按字段排序(int)。如果字段不是int? [英] Order by a field (int). If the field it is not int?
本文介绍了按字段排序(int)。如果字段不是int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到LINQ问题(在C#中)。我需要按字段订购记录列表,该字段应该是 int
,但有时不是:
I have a problem with LINQ (in C#). I need to order a list of records by a field, which should be int
, but sometimes not :
from MyObject obj in new MyObject()
where obj.Visibile=="1"
orderby Int32.Parse(obj.Order) ascending
select obj;
并且,正如我所说,如果obj.Order不是 int
,我收到一个错误: System.FormatException:输入字符串的格式不正确。
and, as I said, if obj.Order is it not int
, I get an error : System.FormatException: Input string was not in a correct format.
我是喜欢将非int项放在列表的末尾,而不会出现任何错误。有可能吗?
推荐答案
尝试使用TryParse
Try by maming use of 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;
}
这篇关于按字段排序(int)。如果字段不是int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文