我有一个 API,其中包含一个带有枚举列表的请求参数。与每个枚举对应的是表中的每个字段。你们可以看看这个:
public enum Type {
a,
b,
c
}
在实体中我有这个:
....
public boolean is_a {get; set;}
public boolean is_b {get; set;}
public boolean is_c {get; set;}
所以我的目的是:当我提交枚举列表是 [a,b] 时,我可以有一个查询可以使用 Where 方法检查以找到记录 is_a
= true 或记录有 is_b
= true。
还有一件事,只是三个字段之一 is_a
、is_b
、is_c
。
更新 只是为了更详细。我像这样给你们我的请求参数:
public class RequestParam {
public string id;
public List<Type> types;
}
我想在这个参数中使用 types
进行查询。
最佳答案
如果我假设您的实体类型称为 Entity
并且您有一个要查询的 entities
列表,那么这对我有用:
var check = new Dictionary<Type, Func<Entity, bool>>()
{
{ Type.a, entity => entity.is_a },
{ Type.b, entity => entity.is_b },
{ Type.c, entity => entity.is_c },
};
var entities = new List<Entity>(); /* populated somehow */
var types = new [] { Type.a, Type.b };
var query =
from entity in entities
where types.Any(type => check[type](entity))
/* use `.All` if you want all props to be true */
select entity;
关于c# - 具有多输入和多字段的 LINQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41438533/