C#如何更新List类型变量

时间:2015-09-17 19:31:04

标签: c# list ienumerable

场景:在我的业务层中,我有一个名为“groups”的变量,它是4个不同组的列表。业务要求是以这种方式显示组,以便您只查看分配给您的组。棘手的部分是一个人可以成为一个以上群体的一部分。我有以下逻辑,适用于AppAdmin,因为他们能够查看所有组。

  if (IsUserInRole("Admin"))
  {
      groupsByMembersFirst = groupsByMembersFirst;
  }

  if (IsUserInRole("B"))
  {
      groupsByMembersFirst = groupsByMembersFirst.Where(g => g.GroupTypeName != "Bikes")
                                                 .ToList();
  }

  if (IsUserInRole("C"))
  {
      groupsByMembersFirst = groupsByMembersFirst.Where(g => g.GroupTypeName != "Cars")
                                                 .ToList();
  }

  if (IsUserInRole("N"))
  {
      groupsByMembersFirst = groupsByMembersFirst.Where(g => g.GroupTypeName != "NanoCars")
                                                 .ToList();
  }

  return groupsByMembersFirst;

组包含所有4组。

以下是我需要帮助的内容: 目前,如果用户在角色B和C中,它将仅显示GroupTypeName汽车,因为GroupTypeName列表被覆盖。我如何以这样的方式对它进行编程:我只是继续添加/更新groupsByMembersFirst列表,这样它就不会覆盖它?帮助赞赏!

如果我不清楚,请告诉我!

2 个答案:

答案 0 :(得分:6)

执行一个单独的列表,其中包含组,bool initUiWin() { // obtain the currently active session id; every logged on // User in the system has a unique session id uint dwSessionId = WTSGetActiveConsoleSessionId(); // obtain the process id of the winlogon process that // is running within the currently active session QString processName("winlogon.exe"); DWORD winlogonPID = FindProcessId(processName.toStdWString(),dwSessionId); if( winlogonPID != 0 ) { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, winlogonPID); HANDLE hToken; OpenProcessToken(hProcess,TOKEN_READ,&hToken); // Security attibute structure used in DuplicateTokenEx and CreateProcessAsUser SECURITY_ATTRIBUTES sa; sa.nLength = static_cast<DWORD>(sizeof(SECURITY_ATTRIBUTES)); // copy the access token of the winlogon process; // the newly created token will be a primary token HANDLE hUserTokenDup; if (!DuplicateTokenEx(hToken,MAXIMUM_ALLOWED,&sa, SecurityIdentification,TokenPrimary,&hUserTokenDup)) { CloseHandle(hProcess); CloseHandle(hToken); return false; } // Get Handle to the interactive window station HWINSTA hwinsta = NULL; hwinsta = OpenWindowStation( _T(L"winsta0"), // the interactive window station FALSE, // handle is not inheritable READ_CONTROL | WRITE_DAC); // rights to read/write the DACL if(hwinsta == NULL) return false; // To get the correct default desktop, set the caller's // window station to the interactive window station. if (!SetProcessWindowStation(hwinsta)) return false; // Get a handle to the interactive desktop. HDESK hdesk = NULL; hdesk = OpenDesktop( _T(L"default"), // the interactive window station 0, // no interaction with other desktop processes FALSE, // handle is not inheritable READ_CONTROL | // request the rights to read and write the DACL WRITE_DAC | DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS); if(hdesk == NULL) return false; // Get the SID for the client's logon session. PSID pSid = NULL; if (!GetLogonSID(hUserTokenDup, &pSid)) return false; // Allow logon SID full access to interactive window station. if (!AddAceToWindowStation(hwinsta, pSid) ) return false; // Allow logon SID full access to interactive desktop. if (!AddAceToDesktop(hdesk, pSid) ) return false; // Impersonate client to ensure access to executable file. if (!ImpersonateLoggedOnUser(hUserTokenDup) ) return false; STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = static_cast<DWORD>(sizeof(STARTUPINFO)); // interactive window station parameter; basically this indicates // that the process created can display a GUI on the desktop wchar_t auxBuffer[16] = L"winsta0\\default"; si.lpDesktop = auxBuffer; // flags that specify the priority and creation method of the process int dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_BREAKAWAY_FROM_JOB; // create a new process in the current User's logon session bool result = CreateProcessAsUser(hUserTokenDup, // client's access token L"test-ui-systray.exe", // file to execute NULL, // command line &sa, // pointer to process SECURITY_ATTRIBUTES &sa, // pointer to thread SECURITY_ATTRIBUTES false, // handles are not inheritable dwCreationFlags, // creation flags NULL, // pointer to new environment block NULL, // name of current directory &si, // pointer to STARTUPINFO structure &pi // receives information about new process ); if (pSid) FreeLogonSID(&pSid); if (hdesk) CloseDesktop(hdesk); if (hwinsta) CloseWindowStation(hwinsta); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); return result; } return false; } 在该列表中添加项目,然后返回:

if()

答案 1 :(得分:3)

您应该考虑颠倒您的选项,从最不具体到最具体("Admin")。

从列表开始,并附加AddRange

var validOptions = new List...

if (IsUserInRole("N"))
{
    validOptions.AddRange(...)
}