用Linq操作数据小记

小记

 1         public void UpdateWarhouse(Administrator admin)
 2         {
 3             var warhouseStr = Request["warhouse"];
 4             if (!string.IsNullOrEmpty(warhouseStr))
 5             {
 6  
 7                 var storageListAll = CacheAccess.GetStorages(_storageContract);
 8  
 9                 int[] selectedId = warhouseStr.Split(',').Select(c => Convert.ToInt32(c)).ToArray();
10                 List<Storage> selectStorages=storageListAll.Where(c=> selectedId.Contains(c.Id)).ToList();
11                 List<Storage> removeStorages = admin.Stoages.Where(c => !selectStorages.Contains(c)).ToList();
12                 List<Storage> newAddStorages = selectStorages.Where(c => !admin.Stoages.Contains(c)).ToList();
13 
14                 foreach (var item in removeStorages)
15                 {
16                     admin.Stoages.Remove(item);
17                 }
18                 foreach(var item in newAddStorages)
19                 {
20                     admin.Stoages.Add(item);
21                 }
22  
23             }
24 
25         }
selectedId 是从画面上取得的选中的值 string 类型 “1,2,3,4”
原文地址:https://www.cnblogs.com/longling2344/p/5865177.html