Click here to Skip to main content
15,896,063 members
Home / Discussions / C#
   

C#

 
GeneralRe: Linq - Remove Object From Collection Pin
Dan Mos8-Dec-11 8:22
Dan Mos8-Dec-11 8:22 
GeneralRe: Linq - Remove Object From Collection Pin
Kevin Marois8-Dec-11 8:24
professionalKevin Marois8-Dec-11 8:24 
GeneralRe: Linq - Remove Object From Collection Pin
Dan Mos8-Dec-11 9:18
Dan Mos8-Dec-11 9:18 
GeneralRe: Linq - Remove Object From Collection Pin
BobJanova8-Dec-11 22:44
BobJanova8-Dec-11 22:44 
Generaltwo compile time errors with your for loop based code: Re: Linq - Remove Object From Collection Pin
BillWoodruff9-Dec-11 9:16
professionalBillWoodruff9-Dec-11 9:16 
GeneralRe: Linq - Remove Object From Collection Pin
BillWoodruff9-Dec-11 9:17
professionalBillWoodruff9-Dec-11 9:17 
AnswerRe: Linq - Remove Object From Collection Pin
Mirko198012-Dec-11 2:53
Mirko198012-Dec-11 2:53 
AnswerRe: Linq - Remove Object From Collection Pin
BillWoodruff9-Dec-11 1:04
professionalBillWoodruff9-Dec-11 1:04 
If the search for the item to remove did not have to be recursive: this is simpler than the other example shown in the answers here:
Folders.Remove(Folders.Find(f => f.id == model.id));
To determine whether a recursive solution is actually required, I think we need to know:

1. is the collection of Models of fixed size, or read in at one time: if it is, then, imho, creating a flattened List<folder> is the way to go: then you can use model.id to get the instance of the Model in the flattened list, and use the simple form of Folders.Remove(modelInstance), and then remove the Model from the flattened list.

2. if the collection is nested only once (a two-level tree), a simpler solution is possible using Linq. See:[^].

3. if the solution is nested to depth > 2, or has arbitrary levels of nesting, a more complex solution in Linq is possible. I suggest you look up the various solutions on StackOverFlow that use Eric Lippert's explanation ... he cites Wes Dyer[^] ... of using Linq to create a flattened hierarchy via the use of a 'Stack' .NET data structure for greatly increased efficiency. See:[^].

4. if the collection of Models is being modified, new Models are being added, etc., frequently, then paying the "cost" of synchronizing (recreating ?) the flattened List may be prohibitive. In that case, well: you just gotta recurse.

The interesting challenge in writing the recursive code in this case is how to immediately, once a matching node has been found, to delete it, and skip iterating its children, given that you can't modify the collection on the fly: which, really, is another reason to consider the use of a "parallel" flattened list, imho. Or you could extend the more common example of using Linq to essentially "temporarily copy" a List, or Dictionary, structure, and then apply the collection modifying operation to the "original" tree ?

... edit 1 ...

Bob Janova posted an interesting way, on this thread, to use a for loop and its indexes to be able to delete from a nested List structure without violating the "no modify collection injunction" found using foreach:[^].

... end edit 1 ...

... edit 2 ...

also see Marc Gravell's recursive Flattening function:[^], which is in the form of an extension.

... end edit 2 ...

Of course, I would really like to see an answer here that used Linq in some way I am not aware of to accomplish this (the depth to which I am unaware of what is possible with Linq ever-deepeningly-unfathomable) Smile | :)
"For no man lives in the external truth among salts and acids, but in the warm, phantasmagoric chamber of his brain, with the painted windows and the storied wall." Robert Louis Stevenson


modified 10-Dec-11 12:34pm.

QuestionType Isomorphisms: this is more an open theory question than a practical programming question Pin
Alaric_8-Dec-11 6:01
professionalAlaric_8-Dec-11 6:01 
AnswerRe: Type Isomorphisms: this is more an open theory question than a practical programming question Pin
Alaric_8-Dec-11 8:09
professionalAlaric_8-Dec-11 8:09 
QuestionCompress and decompress string between java and c# Pin
Hossein Khalaj8-Dec-11 6:00
Hossein Khalaj8-Dec-11 6:00 
AnswerRe: Compress and decompress string between java and c# Pin
Luc Pattyn8-Dec-11 7:25
sitebuilderLuc Pattyn8-Dec-11 7:25 
GeneralRe: Compress and decompress string between java and c# Pin
Hossein Khalaj8-Dec-11 7:40
Hossein Khalaj8-Dec-11 7:40 
AnswerRe: Compress and decompress string between java and c# Pin
Luc Pattyn8-Dec-11 8:39
sitebuilderLuc Pattyn8-Dec-11 8:39 
GeneralRe: Compress and decompress string between java and c# Pin
Hossein Khalaj8-Dec-11 8:48
Hossein Khalaj8-Dec-11 8:48 
AnswerRe: Compress and decompress string between java and c# Pin
Luc Pattyn8-Dec-11 9:06
sitebuilderLuc Pattyn8-Dec-11 9:06 
GeneralRe: Compress and decompress string between java and c# Pin
Hossein Khalaj8-Dec-11 9:10
Hossein Khalaj8-Dec-11 9:10 
QuestionClass to toggle a bool when it goes out of scope Pin
__John_8-Dec-11 3:58
__John_8-Dec-11 3:58 
AnswerRe: Class to toggle a bool when it goes out of scope Pin
PIEBALDconsult8-Dec-11 4:12
mvePIEBALDconsult8-Dec-11 4:12 
AnswerRe: Class to toggle a bool when it goes out of scope Pin
__John_8-Dec-11 5:03
__John_8-Dec-11 5:03 
GeneralRe: Class to toggle a bool when it goes out of scope Pin
harold aptroot8-Dec-11 5:21
harold aptroot8-Dec-11 5:21 
GeneralRe: Class to toggle a bool when it goes out of scope Pin
__John_11-Dec-11 22:15
__John_11-Dec-11 22:15 
GeneralRe: Class to toggle a bool when it goes out of scope Pin
harold aptroot11-Dec-11 22:34
harold aptroot11-Dec-11 22:34 
GeneralRe: Class to toggle a bool when it goes out of scope Pin
__John_12-Dec-11 0:28
__John_12-Dec-11 0:28 
GeneralRe: Class to toggle a bool when it goes out of scope Pin
BobJanova8-Dec-11 5:45
BobJanova8-Dec-11 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.