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

Matt Elhotiby picture Matt Elhotiby · Mar 23, 2012 · Viewed 18.7k times · Source

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

Answer

Adrian Iftode picture Adrian Iftode · Mar 23, 2012
var difference = songs.Except(attributes.Select(s=>s.name)).ToList();

edit

Added ToList() to make it a list