这个插页可以缩短吗?似乎可以跳过foreach。
if (users == null)
{
users = new List<User>();
foreach (UserIDGroupID userIDGroupID in UserIDsGroupIDs.Where(x => x.GroupID == ID))
{
Users.AddRange(LibraryUsers.Where(x => x.ID == userIDGroupID.UserID));
}
}
答案 0 :(得分:3)
您可以尝试将Contains
用于之前选择的ID。样本:
if (users == null)
{
var ids = UserIDsGroupIDs.Select(x => x.UserID);
users = LibraryUsers.Where(x => ids.Contains(x.Id)).ToList();
}