【问题标题】:Dynamic Linq with IN List带有 IN 列表的动态 Linq
【发布时间】:2021-04-05 15:38:22
【问题描述】:

我正在使用 EF Dynamic Linq 库来构建基于用户输入的动态查询。

我能够像这样设置 where 子句来搜索用户角色 = 1:

            var users = await _db.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role)
            .Where(x => x.IsActive == true) 
            .Where("x => x.UserRoles.Any(y => y.RoleId == 1)")

在此查询中,角色 ID“1”来自另一个来源,例如用户输入。如何修改此查询以搜索多个角色 ID?传入的字符串值类似于“1,2,4”。如何在搜索中使用此字符串来查找具有任何角色的所有用户?我尝试使用 new []{1,2,4} 之类的东西,但不确定如何将其放入字符串中。

【问题讨论】:

    标签: linq dynamic-linq dynamic-linq-core


    【解决方案1】:
    int[] roles = new int[] { 1, 2, 3 };
    IEnumerable<User> users = await db.Users
        .Include(x => x.UserRoles)
        .ThenInclude(x => x.Role)
        .Where(x => x.IsActive == true && x.UserRoles.Any(y => roles.Contains(y)));
    

    【讨论】:

    • 对不起,我没有说清楚,由于用户输入的性质,我不得不使用动态 sql 查询。字符串是动态构造的。
    • 好吧,我看错了你的问题。这有帮助吗? stackoverflow.com/questions/2455659/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多