使用LINQ基于字符串中的id获取多个字段

时间:2015-10-05 09:02:04

标签: c# linq linq-to-sql

我有两个表:表1: RolesTable 和表2: PrivilegeTable

RolesTable中的示例数据

Name |Description           |ModulePrivIds|FunctionPrivIds|Active
=================================================================
Role1|This is a Test Role   |1,2          |3,4            |1
Role2|This is another Role  |2,3          |1,3            |0

注意: ModulePrivId FunctionPrivIds 对应 PrivilegeTable

中的 ID

PrivilegeTable

中的示例数据
ID | Name |Description
========================
1  |Priv1 |This is Priv1
2  |Priv2 |This is Priv2
3  |Priv3 |This is Priv3
4  |Priv4 |This is Priv4

现在我想在网格中显示所有角色的数据。因此,我想使用权限表显示权限名称而不是ModulePrivId和FunctionPrivIds。

这是我正在尝试但无法达到预期效果。

var answer = (from r in database.RolesRepository.Get()
            join p in database.PrivilegesRepository.Get()
            on r.ModulePrivilegeIds equals p.Id.ToString() into ModuleWisePrivileges
            from p1 in ModuleWisePrivileges.DefaultIfEmpty()

            from r1 in database.RolesRepository.Get()
            join p01 in database.PrivilegesRepository.Get()
            on r1.FunctionPrivilegeIds equals p01.Id.ToString() into FunctionWisePrivileges
            from p2 in FunctionWisePrivileges.DefaultIfEmpty()

            select new
            {
                Name = r.Name,
                Description = r.Description,
                ModuleWisePrivileges = ModuleWisePrivileges,
                FunctionWisePrivileges = FunctionWisePrivileges,
                Active = r.Active

            }).ToArray();

return answer;

我也试过以下但没有成功。

var answer = (from r in database.RolesRepository.Get()
            join p in database.PrivilegesRepository.Get()
            on p.Id.ToList().Where(p => Id.Contains(r.ModulePrivilegeIds.ToString()) into ModuleWisePrivileges)
            from p1 in ModuleWisePrivileges.DefaultIfEmpty()

            from r1 in database.RolesRepository.Get()
            join p01 in database.PrivilegesRepository.Get()
            on r1.FunctionPrivilegeIds equals p01.Id.ToString() into FunctionWisePrivileges
            from p2 in FunctionWisePrivileges.DefaultIfEmpty()

            select new
            {
                Name = r.Name,
                Description = r.Description,
                ModuleWisePrivileges = ModuleWisePrivileges,
                FunctionWisePrivileges = FunctionWisePrivileges,
                Active = r.Active

            }).ToArray();

2 个答案:

答案 0 :(得分:1)

如果您无法修复数据库,则需要采用多步骤流程:

  1. RolesTable
  2. 中选择行
  3. 拆分ID字段
  4. 从数据库中选择权限
  5. 这样的事情:

    var roles = database.RolesRepository.Get().ToArray();
    var answers = roles.Select(r => new {
                    Name = r.Name,
                    Description = r.Description,
                    ModuleWisePrivileges = r.ModulePrivilegeIds.Split(',')
                                            .Select(x => database.PrivilegesRepository.Get(x))
                                            .ToArray(),
                    FunctionWisePrivileges = r.FunctionPrivilegeIds.Split(',')
                                              .Select(x => database.PrivilegesRepository
                                                                   .Get(x))
                                              .ToArray(),
                    Active = r.Active
                }
    

    如果您的存储库允许您一次获取多个项目,则基于ID列表,可以提高性能 如果特权列表很小,最好先将它们全部放入内存:

    var roles = database.RolesRepository.Get().ToArray();
    var privileges = database.PrivilegesRepository.Get().ToDictionary(x => x.Id, x => x);
    var answers = roles.Select(r => new {
                    Name = r.Name,
                    Description = r.Description,
                    ModuleWisePrivileges = r.ModulePrivilegeIds.Split(',')
                                            .Select(x => privileges[x])
                                            .ToArray(),
                    FunctionWisePrivileges = r.FunctionPrivilegeIds.Split(',')
                                              .Select(x => privileges[x])
                                              .ToArray(),
                    Active = r.Active
                }
    

答案 1 :(得分:0)

检查一次,用逗号分隔值更新上述代码

 var roles = database.RolesRepository.Get().ToArray();
 var answers = roles.Where(a => a.isDeleted == false).Select(r => new {
        Name = r.Name,
        Description = r.Description,
        ModuleWisePrivileges = string.Join(", ", (r.ModulePrivilegeIds.Split(',')
                                .Select(x => database.PrivilegesRepository.Get(x).FirstOrDefault())
                                .ToArray()),
        FunctionWisePrivileges = string.Join(", ", (r.FunctionPrivilegeIds.Split(',')
                                  .Select(x => database.PrivilegesRepository.Get(x).FirstOrDefault())
                                  .ToArray()),
        Active = r.Active
    }

根据您的评论更新了查询

{{1}}