我只有一个对象(数据库中的一条记录),并希望判断该对象是否匹配以Func<T, bool>
为形式的给定条件.
我通过将对象添加到对象列表中来解决这个问题,并try 使用where子句从这个列表中获取它,给定如下条件
var testMatch = new Test
{
t1 = "123"
};
// Query is stored in a string and compiled with Sprint.Filter.OData to p => p.t1 == "123"
var query = "t1 eq '123'";
var compiledQuery = Filter.Deserialize<Test>(query).Compile();
// This works like expected
Console.WriteLine(Match<Test>(compiledQuery, testMatch));
public bool Match<T>(Func<T, bool> condition, T testObject)
{
var test = new List<T>();
test.Add(testObject);
var t = test.Where(condition).FirstOrDefault();
return (t != null);
}
public class Test
{
public string t1 {get; set;}
}
因此,这符合预期,但我想知道是否有比创建列表、将其添加到此列表并try 使用条件检索它更简单的方法来测试单个对象.
我是不是错过了什么很简单的东西?