This is my model:
public class Combination
{
public int Id { get; set; }
public int CombOne{ get; set; }
public int CombTwo{ get; set; }
}
I want to write a LINQ statement to extract those instances of Combination class which contains a duplicate combination of properties CombOne and CombTwo. So if there are three instances like:
Combination C1= new Combination{Id=1, CombOne=1, CombTwo=2};
Combination C2= new Combination{Id=2, CombOne=2, CombTwo=1};
Combination C3= new Combination{Id=3, CombOne=1, CombTwo=2};
Linq statement should return a list of C2, and C3 since they contain a duplicate combination of CombOne and CombTwo variables, at the same time it should preserve the original instance(C1)(Should not return C1 as it is the first instance of that combination.)
I got correct result with a foreach loop.
List<Combination> invalid2 = new List<Combination>();
foreach (Combination g in list)// Gamelist.Match is a list of Combination type
{
if (invalid2.Contains(g))
continue;
List<Combination> invalid3 = (from r in list
where
((r != g) &&
(((r.CombOne == g.CombOne) && (r.CombTwo == g.CombTwo)) ||
((r.CombOne == g.CombTwo) && (r.CombTwo == g.CombOne))))
select r).ToList();
invalid2 = invalid2.Concat(invalid3).ToList();
}
I would like to get the result using only Linq statements to improve efficiency. I tried a lot but did not get the desired output. Thanks in advance for your sincere effort