Since you stated in your reply to Original Griff that you cannot change the design, here's what I'd do using the classes as you described:
class Artist
{
String ArtistName {get;set;}
}
class Albums
{
String ArtistName {get;set;}
String AlbumName {get;set;}
}
Assuming the names of the variables of the two Lists are:
AllAlbums
and
AllArtists
Then, to delete the Artists for which there are no albums:
HashSet<string> AlbumArtistSet = new HashSet<string>(AllAlbums.Select(al => al.ArtistName));
var trimmedArtists = AllArtists.Where(a => AlbumArtistSet.Contains(a.ArtistName));
This makes only one pass through each list.
=================
Edit: MTH
Since the data is being reparsed from JSon, why not reparse it into more useful data structures along the line of what Original Griff proposed?
Or, if you aren't using a DB yourself, then why not setup something like:
Dictionary<string, List<Albums>> AlbumsByArtistName = new Dictionary<string, List<Albums>>();
foreach (var al in AllAlbums)
{
string name = al.ArtistName;
List<Albums> albums;
if (!AlbumsByArtistName.TryGetValue(name, out albums)
{
albums = new List<Albums>();
AlbumsByArtistName.Add(name, albums);
}
albums.Add(al);
}
var trimmedArtists = AllArtists.Where(a => AlbumsByArtistName.Contains(a.ArtistName));
Still only one pass through each collection, but now you can access the subset collection of albums by artist name. You can setup similar dictionaries for other properties that you might want subset access. Just remember to add and delete
everywhere appropriately.
At this point you've probably noticed that this is looking a bit like a DB. :-)
Using your own in-process (in-memory?) DB, independent from the data source, might be the best solution.