简体   繁体   中英

How to get comparison of variable in lists as objects of class contained in another list?

Class contained in list:

List<Artists> TopArtistsByTags = new List<Artists>();

That class:

public class Artists
{
    public string ArtistName { get; set; }
    public List<string> Tags;
}

I hope to get all ArtistName by comparison of matching duplicates of items in List<string> Tags (Maybe with this items as class? So i'm going to add them both as items and subitems to listView ).

I think this is what you're looking for:

List<Artists> artists = //the list of artists
var tags = artists.SelectMany(x => x.Tags).Distinct();
foreach (var tag in tags)
{
    var artistsWithThisTag = artists.Where(x => x.Tags.Contains(tag));
    //do something with artistsWithThisTag
}

The performance of this could surely be improved, but you should only concern yourself with that if you find it is a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM