簡體   English   中英

linq2sql Group-By then Where 的問題

[英]Problem with linq2sql Group-By then Where

我正在嘗試獲取沒有當前角色的用戶記錄。 一個用戶可以有多個角色。

我正在處理 2 個表 - 用戶和 UserRoleMap

var superAdminRoleId = *GUID*

var query = from user in _userRepository.Table
            join userRoleMap in _userRoleMapRepository.Table
            on user.Id equals userRoleMap.UserId
            // Now, group the roles by the user
            group userRoleMap by user into userRoleMaps
            // get the records that are NOT super admin
            where !userRoleMaps.Any(map=>map.UserId == superAdminRoleId)
            select userRoleMaps.Key;

我收到錯誤

LinqToDB.LinqToDBException: ''map.UserId' 無法轉換為 SQL。

所以我把它修改為

var query = from user in _userRepository.Table
            join userRoleMap in _userRoleMapRepository.Table
            on user.Id equals userRoleMap.UserId
            // Now, group the roles by the user
            group userRoleMap.UserId by user into userRoleMaps   // changed userRoleMap to userRoleMap.UserId 
            // get the records that are NOT super admin
            where !userRoleMaps.Any(map=>map == superAdminRoleId) // update
            select userRoleMaps.Key;

現在,我得到

System.ArgumentException: 未為類型“System.Guid”(參數“property”)定義“屬性”“System.Guid Id””

可能有一種方法可以group by來修復您的group by ,但是看看您正在嘗試完成的工作,我認為您將獲得性能更好且更簡單的查詢,如下所示:

var userRoleMaps = _userRoleMapRepository.Table;
var nonAdminUsers = _userRepository.Table
    .Where(user => !userRoleMaps
        .Any(map => map.UserId == user.Id && map.RoleId == superAdminRoleId));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM