你可以使用 Int.TryParse
var cardsList = cardList.Where(_item => {
int conv;
return int.TryParse(_item.CARD_MANA_CONVT, out conv) &&
conv > 5;
});
如果 X 应该是 >5 则
var cardsList = cardList.Where(_item => {
int conv;
return _item.CARD_MANA_CONVT == "X" ||
(
int.TryParse(_item.CARD_MANA_CONVT, out conv) &&
conv > 5
);
});
如果您发现这使您的查询更难阅读,那么您可以提取到扩展方法。
public static class ManaHelper {
public static bool IsGreaterThan(this string mana, int value, bool includeX) {
if (mana == "X") return includeX;
int manaValue;
return int.TryParse(mana, out manaValue) && manaValue > value;
}
}
然后你可以这样做:
var cardsList = cardList.Where(_item => _item.CARD_MANA_CONVT.IsGreaterThan(5,false));
或包含X
在结果中
var cardsList = cardList.Where(_item => _item.CARD_MANA_CONVT.IsGreaterThan(5,true));