从Observable集合中删除项目 [英] Remove an item from Observable collection
问题描述
我有一个Radgridview,我正在使用复选框从中删除项目。但是当我删除它时总是删除我选择的第一个值。
I have a Radgridview and i am deleting items from it using a checkbox.But when i delete it is always deleting the first value not which i have selected.
var takeoffsSelected = TakeOffInfoCollection.ToList().Where(x => x.IsSelected);
//Deleted Takeoff
foreach (var takeoff in takeoffsSelected)
{
objTakeOffModel.DeleteRecordFromDB(SageVariables.Project_ID, SageVariables.CurrentQuoteID, takeoff.DeleteID);
TakeOffInfoCollection.Remove(takeoff);
}
推荐答案
您好b $ b
第一:您的代码当你只需要枚举(并且顺序错误)时,不需要转换到列表。
所以如果你需要枚举你的结果(并且你在之后再次枚举它)那个...)
你会写:
Hi
First: Your code does unneeded conversion to a list while you just need enumeration (and in the wrong order too).
So if you need your result enumerated (and you enumerate it again after that...)
you would write:
var takeoffsSelected = TakeOffInfoCollection.Where(x => x.IsSelected).ToList();
所以你在搜索匹配元素后将结果转换为列表。
你现在做的是:
首先将所有元素枚举到新列表,然后在内存中(不是通过原始查询提供程序)使用LinqToObjects来搜索列表。然后你枚举结果。 - 你看这是不太好......
只需写下:
So you transform the result to a list after searching the matching elements.
What you do now is:
First enumerate all elements to a new list, then in Memory (not via the original query provider) you use LinqToObjects to search the list. then you enumerate the results. - You see this is "not so good"...
Just write:
var takeoffsSelected = TakeOffInfoCollection.Where(x => x.IsSelected);
foreach (var takeoff in takeoffsSelected)
...
所以你当前的代码效率不高(取决于转换所有项目的成本)到内存列表...)但应该工作。所以问题不在你展示的代码中。 - 也许复选框没有正确设置IsSelected propery或类似的东西,调试和看....
亲切问候Johanne
So your current code is not very efficient (depends on the cost of transforming all items to a in-Memory list...) but should work. So the Problem is not in the code you are showing. -Maybe the checkboxes don't set the IsSelected propery correctly or something like that, debug and see....
Kind regards Johanne
这篇关于从Observable集合中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!