Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
601 views
in Technique[技术] by (71.8m points)

linq - How do i get the difference in two lists in C#?

Ok so I have two lists in C#

List<Attribute> attributes = new List<Attribute>();
List<string> songs = new List<string>();

one is of strings and and one is of a attribute object that i created..very simple

class Attribute
{
    public string size { get; set; }
    public string link { get; set; }
    public string name { get; set; }
    public Attribute(){}
    public Attribute(string s, string l, string n) 
    {
        size = s;
        link = l;
        name = n;
    }
}

I now have to compare to see what songs are not in the attributes name so for example

songs.Add("something"); 
songs.Add("another"); 
songs.Add("yet another");

Attribute a = new Attribute("500", "http://google.com", "something" ); 
attributes.Add(a);

I want a way to return "another" and "yet another" because they are not in the attributes list name

so for pseudocode

difference = songs - attributes.names
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
var difference = songs.Except(attributes.Select(s=>s.name)).ToList();

edit

Added ToList() to make it a list


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...