linq 两个list交集差集处理

 1  public object QuitChat(string groupId, string receiveMark)
 2         {
 3             try
 4             {
 5                 //用户Mark转换
 6                 List<string> receiveMarks = receiveMark.Split(',').ToList();
 7                 if (receiveMarks.Count > 0)
 8                 {
 9                     //组和用户Mark判断
10                     List userMark = _groupMemberManageAccess.GetModelListByGroupId(groupId);
11 
12                     if (userMark != null && userMark.Count > 0)
13                     {
14                         //组成员信息
15                         var userMarks = userMark.Select(t => t.UserMark).ToList();
16                         //交集
17                         var intersect = receiveMarks.Intersect(userMarks).ToList();
18                         if (intersect.Count == 0)
19                         {
20                             return new
21                             {
22                                 code = (int)ResultCodeEnum.ParameterError,
23                                 msg = "receiveMark=" + string.Join(",", receiveMarks) + "参数不对!"
24                             };
25                         }
26                         //差集
27                         var except = receiveMarks.Except(intersect).ToList();
28 
29                         if (except.Count > 0)
30                         {
31                             return new
32                             {
33                                 code = (int)ResultCodeEnum.ParameterError,
34                                 msg = "receiveMark=" + string.Join(",", except) + "参数不对!"
35                             };
36                         }
37                         else
38                         {
39                             //被移除的组信息更新为不可用
40                             foreach (var item in receiveMarks)
41                             {
42                                 var groupMemberManage = userMark.FirstOrDefault(t => t.UserMark == item);
43                                 if (groupMemberManage != null)
44                                 {
45                                     _groupMemberManageAccess.Update(groupMemberManage.Id,
46                                         0); //review 七月:将此方法修改为只根据ID更新状态。
47                                 }
48                             }
49 
50                             RemoveDialogGroupSearchModel removeDialogGroupSearchModel = new RemoveDialogGroupSearchModel
51                             {
52                                 GroupId = groupId,
53                                 UserMark = receiveMark
54                             };
55                             //调用聊天服务移除接口
56                             return RemoveDialogGroup(removeDialogGroupSearchModel);
57                         }
58                     }
59                     else
60                     {
61                         return new
62                         {
63                             code = (int)ResultCodeEnum.ParameterError,
64                             msg = "receiveMark=" + string.Join(",", receiveMarks) + "参数不对!"
65                         };
66                     }
67                 }
68             }
69             catch (Exception ex)
70             {
71                 LogHelper.WriteLogForDll("结束会话接口,可指定人员退出", ex, $"groupId:{groupId}/receiveMark:{receiveMark}");
72                 return new
73                 {
74                     code = (int)ResultCodeEnum.SystemException,
75                     msg = "系统异常!"
76                 };
77             }
78 
79             return new
80             {
81                 code = (int)ResultCodeEnum.RequestSucess,
82                 msg = ""
83             };
84         }
View Code

 

转载于:https://www.cnblogs.com/fyy1003668657/p/11107197.html

你可能感兴趣的:(linq 两个list交集差集处理)