Click here to Skip to main content
15,887,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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);
            }
Posted
Comments
Rob Philpott 9-Feb-15 8:08am    
What type is takeoff? That's the problem with var, it leaves us with no idea.

I suspect you probably have an equality issue on the type, whereby you are expecting it to perform comparisons by the objects reference, but its using the Equals() method on the type. Check your identity logic.
chandra sekhar 9-Feb-15 8:13am    
takeoff is TakeOffInfoDomain
Kornfeld Eliyahu Peter 9-Feb-15 9:08am    
Change var to the actual type...
chandra sekhar 9-Feb-15 9:10am    
Changed but still no use.
Kornfeld Eliyahu Peter 9-Feb-15 9:55am    
When you use LINQ on observable collection the result no more observable...So probably the types are not fit anymore...
Have you checked the runtime-type of your vars?

1 solution

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:

C#
var takeoffsSelected = TakeOffInfoCollection.Where(x => x.IsSelected).ToList();


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:

C#
var takeoffsSelected = TakeOffInfoCollection.Where(x => x.IsSelected);
foreach (var takeoff in takeoffsSelected)
                ...


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
 
Share this answer
 
Comments
chandra sekhar 10-Feb-15 1:15am    
The whole problem came with the Index we are not deleting the value properly and Remove failed to do that and the solution is

var takeoffsSelected = TakeOffInfoCollection.ToList().Where(x => x.IsSelected);
for (int i = TakeOffInfoCollection.Count-1; i >= 0 ; i--)
{
if (TakeOffInfoCollection[i].IsSelected == true)
TakeOffInfoCollection.RemoveAt(i);
}
johannesnestler 10-Feb-15 6:50am    
I see... My normal way is - search all indices to delete, then delete: like this:

// var is IEnumerable<int>
var listofIndices = TakeOffInfoCollection
.Select((x, index) => new { index, element = x }) // project into a new type with the indices
.Where(x => x.element.IsSelected) // filter out just the selected elements
.Select(x => x.index); // project just the indices as result

// Loop just the list of Indices to delete each item
foreach (int index in listofIndices)
TakeOffInfoCollection.RemoveAt(index);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900