【发布时间】:2015-12-18 19:28:33
【问题描述】:
使用空引用检查过滤值的最简单方法是什么。顺序应该类似于“活动”, “重启”, “锁定”, “暂停 ”, “已到期”, “残疾”, “撤销”
namespace ConsoleApplication1
{
class Program
{
private static void Main(string[] args)
{
var tempList = new List<string>
{
"Active",
"Reset",
"Locked",
"Suspended ",
"Expired",
"Disabled ",
"Revoked"
};
var list = new List<MyEntity>
{
new MyEntity() {MyValue = "Reset"},
new MyEntity() {MyValue = "Locked"},
new MyEntity() {MyValue = "Active"},
new MyEntity() {MyValue = "Expired"}
};
var item =
list.FirstOrDefault(x => x.MyValue));
}
}
public class MyEntity
{
public string MyValue { get; set; }
}
}
我需要做什么才能根据值过滤列表...
【问题讨论】:
-
如果列表中没有
MyValue为“A”的项目,那么只选择一个MyValue为“B”的项目?还有MyValue的确切类型是什么? -
var 过滤 = list.Sort().FirstOrDefault();应该做的伎俩
-
@juharr 你是对的
-
好的,如果没有 A、B、C 或 D,那么您想要
null对吗? -
你不应该编辑你的问题作为解决方案。