0

I have a list from a customer class that is shown below

   internal class RMDocument
    {
        public string FolderProfileNumber { get; set; }

        public string FolderName { get; set; }
        public List<FolderItem> Documents { get; set; }

    }

and as you see, there is a list from FolderItem. and FolderItem has DocNumber property.

so I want to delete item which has the same document number with Documents items.

foreach (var dup in DocumentsAndParents.Where(d => !(d.Value.Count == 1 && d.Value[0].ReferenceCount == 1)))
{
    var duplicated = ExportDocumentsToRm.SelectMany(s =>
    s.Documents).FirstOrDefault(f => f.DocNumber.Equals(dup.Key));

    if (duplicated != null)
   {
     ExportDocumentsToRm.SelectMany(s => s.Documents).ToList()
    .RemoveAll(r => r.DocNumber.Equals(duplicated.DocNumber));

   }
}

with code I think my question is clear. Well I know the problem, I used ToList() thats why it doesnt remove, but otherwise I cant access RemoveAll or other Remove methods.

How I can remove items if docnumber is the same with anitem from Documents. I just want to remove item from the "Documents" not from the ExportDocumentsToRm, It has Documents property as a list

unbalanced
  • 1,192
  • 5
  • 19
  • 44
  • Could you expand the classes at the top a bit better? Are you effectively saying you want to delete all documents in `documents` if the name matches `FolderName`? – Heberda Nov 03 '15 at 13:08
  • you can think FolderItem has one property named DocNumber, and DocumentsAndParents is just a list which contains duplicated document number.. so with a loop I am looking if the docnumber is in my documents list, if yes, i want to delete that item – unbalanced Nov 03 '15 at 13:11
  • have you seen this http://stackoverflow.com/a/4503090/3002953 – Heberda Nov 03 '15 at 13:24
  • no but i dont want to use loop.. I tried someting now and seems it is working ExportDocumentsToRm.Any(r => r.Documents.Remove(duplicated)); – unbalanced Nov 03 '15 at 13:26
  • I don't think your solution would handle 3 duplicate documents? – Heberda Nov 03 '15 at 13:30
  • I think its okay, for each loop it looks again the document list., – unbalanced Nov 03 '15 at 14:07
  • So you'd run it twice? That's quite inefficient? – Heberda Nov 03 '15 at 14:08
  • yes I think it is.. I should think about it more.. on the code, you can see the first loop has all documents and in the loop it looks all time my list that has document list. As you said it is not efficient – unbalanced Nov 03 '15 at 14:10
  • By using a `Group By` and then `Skip(1)` you should be able to sort it quite easily :) I'm just a bit confused by your models haha – Heberda Nov 03 '15 at 14:15
  • It is not actually whole my code :) I just add something into the code.. yes gropping can be a solution for the loop, thank you :) – unbalanced Nov 03 '15 at 14:23

0 Answers0