Remove data from sub list

Viewed 70

I created this sample project where I am trying to remove sub data using the Where and Any but it still contains the data. It should remove the DD notes and just keep the CC notes.

Any help on this would be great.

TemplateNote = TemplateNote.Where(x =>
    x.Content == "AA" &&
    x.Conditions.Any(v => v.Target != removeAgentDisease)
).ToList();

https://dotnetfiddle.net/a3d6Py

current Output includes the DD notes

TemplateNote AA
TemplateNoteCondition CC
TemplateNoteCondition DD

Should be this without the DD

TemplateNote AA
TemplateNoteCondition CC
1 Answers

You are removing from the parent list, not from the child list. Therefore you have to create a copy-TemplateNoteModel with a new child-list that just contains the desired items:

TemplateNote = TemplateNote
    .Select(tn => new TemplateNoteModel
    {
        Conditions = tn.Conditions
           .Where(c => c.Target != removeAgentDisease)
           .ToList(),
        Content = tn.Content,
        Id = tn.Id
    })
    .Where(x => x.Conditions.Any()) // i guess you don't want empty condtions
    .ToList();

Your modified fiddle: https://dotnetfiddle.net/yduh5o

Related