Remove an item from a LINQ collection
我想我的问题是正确的:)
我正在使用LINQ查询数据库以检索一些数据。请找到下面的代码。
1 2 3 4 5 6 7 8 | var leadtasktype = _context.LeadTypeTaskTypes.Where(l => l.LeadTypeId == item.Value); foreach(LeadTypeTaskType l in leadtasktype){ if (l.TaskTypeId == 21) { //I need to remove an item which has the tasktype id 21 } } |
正如我在评论中提到的,我需要根据我的
这看起来很像另一个过滤条件:
1 2 3 | var leadtasktype = _context.LeadTypeTaskTypes .Where(l => l.LeadTypeId == item.Value) .Where(l => l.TaskTypeId != 21); // Exclude ID = 21. |
把它们放在你的设备里就行了:
1 2 3 4 5 | var leadtasktype = _context.LeadTypeTaskTypes .Where(l => l.leadTypeId != 21 && l.LeadTypeId == item.Value ); |
这就是你想要的吗?
1 2 | var leadtasktype = _context.LeadTypeTaskTypes.RemoveAll (l => l.LeadTypeId == item.Value && l.LeadTypeId == 21); |
1 2 3 | var leadtasktype = _context.LeadTypeTaskTypes .Where(l => l.LeadTypeId == item.Value && l.TaskTypeId != 21); // Exclude ID = 21. |
1 | var leadtasktype = _context.LeadTypeTaskTypes.Where(l => l.LeadTypeId == item.Value && l.LeadTypeId != 21); |
1 2 3 4 5 6 7 8 9 | var leadtasktype = _context.LeadTypeTaskTypes.Where(l => l.LeadTypeId == item.Value); for (var i = 0; i < leadtasktype.Count; i++) { if (leadtasktype[i].TaskTypeId == 21) { leadtasktype.RemoveAt(i); } } |
1 2 3 4 5 6 7 8 9 10 | var leadtasktype = _context.LeadTypeTaskTypes.Where(l => l.LeadTypeId == item.Value); var newData = leadtasktype; foreach(LeadTypeTaskType l in leadtasktype){ if (l.TaskTypeId == 21) { newData.Remove(l); } } |