使用空引用检查过滤值的最简单方法是什么.Order应该像"Active","Reset","Locked","Suspended","Expired","Disabled","Revoked"
namespace ConsoleApplication1 { class Program { private static void Main(string[] args) { var tempList = new List{ "Active", "Reset", "Locked", "Suspended ", "Expired", "Disabled ", "Revoked" }; var list = new List { 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; } } }
我需要做什么才能根据值将过滤器列入清单...
这听起来像你想做的OrderBy
,如果你想要的是Sam,然后是Paul,然后是Jimmy,那么Jeff,然后null
如果没有这些,那么你可以做以下事情.
var listOfNames = new List{ "Sam", "Paul", "Jimmy", "Jeff" }; var item = list.Where(x => listOfNames.Contains(x.MyValue)) .OrderyBy(x => listOfName.IndexOf(x.MyValue)) .FirstOrDefault();
这将首先筛选出与您感兴趣的值不匹配的任何内容.然后按照他们在列表中的位置对它们进行排序,最后选择第一个或者null
过滤器没有匹配.
此外,我只是假设MyValue
是在string
这里,但如果你需要,你可以做任何需要的转换.