Reputation: 671
I have the following classes ( simplified ):
public class SeguradoraCalculo
{
public int CodigoSeguradora { get; set; }
public List<Franquia> Franquias{get;set;}
}
public class Franquia {
public int CodFranquia{get;set;}
}
And i have the following data:
var allData = new List<SeguradoraCalculo>(){new SeguradoraCalculo(){
CodigoSeguradora = 11,
Franquias = new List<Franquia>()
{
new Franquia()
{
CodigoFranquia = 1
},
new Franquia()
{
CodigoFranquia = 2
}
}
}
};
var except = new List<SeguradoraCalculo>()
{
new SeguradoraCalculo()
{
CodigoSeguradora = 11,
Franquias = new List<Franquia>()
{
new Franquia()
{
CodigoFranquia = 1
}
}
}
};
How can i have a result that, removing items that match CodigoSeguradora
and CodigoFranquia
, existing Franquias are removed and i get SeguradoraCalculo
only if i have at least one Franquia
in my Franquias
List?
In this example my final list would be:
CodigoSeguradora = 11,
Franquias = new List<Franquia>()
{
new Franquia()
{
CodigoFranquia = 2
}
}
Thanks
Upvotes: 2
Views: 1494
Reputation: 203830
First join the two collections, based on the key, then go through each joined pair and remove all items from the first that are contained in the second:
var query = allData.Join(except,
item => item.CodigoSeguradora,
item => item.CodigoSeguradora,
(a, b) => new { a, b });
foreach (var pair in query)
pair.a.Franquias.RemoveAll(f =>
pair.b.Franquias.Select(x => x.CodFranquia).Contains(f.CodFranquia));
Upvotes: 2