[英]How to remove an element from an IGrouping
How do I remove an object directly from an IGrouping IGrouping<DateTime, VMAppointment>
? 如何直接从IGrouping
IGrouping<DateTime, VMAppointment>
删除对象?
The only way I know of currently is to generate a new IGrouping without the concering element, but I don't like this way because it causes some trouble within my application. 我目前唯一知道的方法是生成一个没有concering元素的新IGrouping,但我不喜欢这种方式,因为它在我的应用程序中引起了一些麻烦。
Any ideas? 有任何想法吗?
No, there's no way to mutate an IGrouping<,>
, at least in general - and even if you knew the concrete type, I don't believe any of the implementations exposed by the .NET framework allow the group to be mutated. 不,没有办法改变
IGrouping<,>
,至少在一般情况下 - 即使你知道具体的类型,我也不相信.NET框架公开的任何实现都允许组变异。
Presumably the grouping is the result of some query - so if possible, change the original query to exclude the values you aren't interested in. 据推测,分组是某些查询的结果 - 因此,如果可能,请更改原始查询以排除您不感兴趣的值。
I know this is old question, but hopefully this helps someone else. 我知道这是一个老问题,但希望这有助于其他人。 A workaround for this is to cast the group to a list, then use the values from the list instead of the group.
解决方法是将组强制转换为列表,然后使用列表中的值而不是组。
var groups = someList.GroupBy(x => x...);
foreach (var group in groups)
{
var groupList = group.ToList();
...
groupList.Remove(someItem);
//Process the other code from groupList.
}
You could cast using Select
and use TakeWhile
if you have a testable condition (such as null as in the example) on a property in your group: 如果您在组中的属性上具有可测试条件(如示例中的null),则可以使用
Select
TakeWhile
并使用TakeWhile
:
var removedItemsList = group.Select(x => x.TakeWhile(t => t.someProperty != null));
This will return an IEnumerable<IEnumerable<YourGroup>>
. 这将返回
IEnumerable<IEnumerable<YourGroup>>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.