This is for .NET 2.0, so I cannot use LINQ.
I have a bit of an interesting problem.
I am merging two lists of custom type “Article”.
The below code does the job nicely:
List<Article> merge = new List<Article>(GetFeatureArticles());
merge.AddRange(result);
return merge;
GetFeatureArticle has only 2 items that are the first two elements in the merged list.
“result” is large and its elements trail “GetFeatureArticle”‘s elements.
The problem is that I need to compare the list returned from “GetFeatureArticles()”
to the list in “result” and, if there is a match, remove the matched item in result, not
in “GetFeatureArticles”. Both lists are of type List<Article>.
I am limited by C# 2.0 unfortunately.
Thank you.
EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT
This is the implementation I ultimately went with as GetFeaturedArticles()
will always be two items:
List<Article> k = new List<Article>(GetFeatureArticles());
foreach (Article j in k)
{
for( int i = 0; i < tiles.Count; i++ )
{
if (j.ID == tiles[i].ID)
tiles.Remove(tiles[i]);
}
}
k.AddRange(tiles);
return k;
use
EDIT – as per comments:
I assume that
Articleis a reference type and is implemented with suitableEqualsandGetHashCodemethods respectively – otherwise the above would only work for equal references (= “same object in both Lists”)…