long outValue;
//will work, but double conversion
var result = TheArrayList.Cast<object>()
.Where(m => Int64.TryParse(m.ToString(), out outValue))
.Select(m => Convert.ToInt64(m)).ToList();
//should avoid double Parse, but untested, see Daniel Hilgarth's answer and warnings.
var result = TheArrayList.Cast<object>()
.Where(m => Int64.TryParse(m.ToString(), out outValue))
.Select(m => outValue).ToList();
或舊的foreach,這可能是最好的選擇。
var list = new List<long>();
long outValue;
foreach (object value in the ArayList) {
if (Int64.TryParse(value.ToString(), out outValue))
list.Add(outValue);
}
特別是因爲這將雙重轉換,不是嗎? –
@ChrisSinclair是的。 –
您可以通過'選擇'm => outValue'來避免雙重轉換,但是這會引發一些WTF。 – Rawling