這是前一陣子寫的程式,一時想找還找不太到....記錄一下:
關鍵:Linq 的 Aggregate (合計)
關鍵:Linq 的 Aggregate (合計)
//將互斥的roleId 由IEnumerable<HashSet<string>> 轉換成 HashSet<string> //Union後的RoleIds 參考:http://goo.gl/5rl0LY
var listOfLists = AllRoleMutexs.Value.Where(m => m.Mutex.RoleIds.Contains(relation.RoleID)).Select(m => m.Mutex.RoleIds);
if (listOfLists.Any())
{
var mutexRoleIds = listOfLists.Skip(1)
.Aggregate(new HashSet<string>(listOfLists.First()),
(h, e) => { h.UnionWith(e); return h; });//取聯集
var relationChecks = ServiceFactory.SecModifier.GetRoleRelationByAccountAndRoleIds(relation.AccountID, mutexRoleIds.ToArray());
if (relationChecks.Any())
{
throw new ArgumentException(string.Format("此[{0}]角色 與目前帳號[{2}]存在的[{1}]角色互斥,無法建立關聯", relation.RoleID, string.Join(",", relationChecks.Select(m => m.RoleID)), relation.AccountID));
}
}

0 意見 :
張貼留言