Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 841
Matt Elhotiby Avatar asked Mar 23 '12 14:03

Matt Elhotiby


People also ask

How do you find the difference between two lists?

The difference between two lists (say list1 and list2) can be found using the following simple function. By Using the above function, the difference can be found using diff(temp2, temp1) or diff(temp1, temp2) .

How do you find the difference between two lists in Scala?

In Scala Stack class , the diff() method is used to find the difference between the two stacks. It deletes elements that are present in one stack from the other one. Return Type: It returns a new stack which consists of elements after the difference between the two stacks.


1 Answers

var difference = songs.Except(attributes.Select(s=>s.name)).ToList();

edit

Added ToList() to make it a list

like image 137
Adrian Iftode Avatar answered Sep 20 '22 08:09

Adrian Iftode