简体   繁体   English

如何将列表中的变量作为另一个列表中包含的类的对象进行比较?

[英]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 ). 我希望通过比较List<string> Tags中项目的匹配重复项来获取所有ArtistName (也许将此项目作为类?所以我将它们既作为项目又作为子项目添加到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. 它的性能肯定可以得到改善,但是如果发现问题,则应该只关心它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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