2015-10-05 56 views
0

我有兩個表:表1:角色表和表2:特權表使用LINQ獲取基於字符串中多個字段的字段使用LINQ

在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 

注:ModulePrivIdFunctionPrivIds對應IDSPrivilegeTable

樣本數據中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(); 
+1

爲什麼不簡單地使用'特權'和'角色'之間的'm:n'關係?我認爲這是不可能的,沒有某種黑客攻擊(即:sql視圖會讓它成爲'm:n' ...然後你遇到插入問題)或者將整個表格加載到你的應用程序並且在本地處理它(那就是你在代碼中試圖做什麼,這有點緩慢和頭痛)... –

+1

你知道這是一個不好的數據庫佈局?良好的數據庫佈局將使用RolesTable和PrivilegesTable之間的映射表。這樣,你可以使用標準的導航屬性。 –

+0

@DanielHilgarth:我建議他們改變數據庫佈局,但他們似乎不想更改數據庫。你周圍的任何工作都可以建議? –

回答

1

如果您不能修復數據庫,如果只剩下一個多步驟的過程:

  1. RolesTable
  2. 拆分選擇行ID字段
  3. 從選擇的特權數據庫

類似這樣的:

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 
      } 
+0

您好Daniel, 我在嘗試運行這段代碼時遇到異常。 –

+0

@gauravsachdeva:你需要給我例外,否則我不能幫你。 –

+0

嗨丹尼爾, 我收到一個異常,而試圖運行這段代碼。 例外說「給定的密鑰不在字典中。」 –

0

入住這一次,更新了上面的代碼用逗號分隔值

var roles = database.RolesRepository.Get().ToArray(); 
var answers = roles.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 
     } 

更新根據你的意見查詢

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 
    } 
+0

嗨Suresh ...有沒有辦法在上面的選擇添加一個where子句。我希望查詢檢查isDeleted標誌是true還是false,並且只獲取isDeleted爲false的結果。 –

+0

用where條件更新了查詢isDeleted爲false – Suresh