c# - 具有多输入和多字段的 LINQ

标签 c# linq enums

我有一个 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_ais_bis_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/

相关文章:

c# - 如何在 WPF 中暂停绑定(bind)的 UI 更新?

c# - Java 中有类似 C# Task 的东西吗?

c++ - 将整数类型转换为枚举 : functional cast vs initialization

java - 如何为枚举的每个条目添加描述?

c# - WPF:在 XP 上有 xamlparse 异常但在 Vista 上没有的任何原因?

c# - 如何使用 C# 清空 Gmail 垃圾箱

.net - 在 EF5 的 select select 子句中选择 null

c# - 错误 : LINQ to Entities does not recognize the method DataLength

c# - 查询和映射复杂对象

java - 带有构造函数的枚举并在 switch JAVA 中使用它