Take the 2-minute tour ×

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("yet another");

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

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
share|improve this question
add comment

4 Answers

up vote 20 down vote accepted
var difference = songs.Except(attributes.Select(s=>s.name)).ToList();


Added ToList() to make it a list

share|improve this answer
will difference be a list –  Trace Mar 23 '12 at 14:12
no, is IEnumerable, but call ToList() –  Adrian Iftode Mar 23 '12 at 14:13
An enumerable. Use ToList() if you need to. –  DeeMac Mar 23 '12 at 14:13
Sorry Adrian, beat me to it. This is actually a really good answer, been wondering how to achieve this for a while myself. +1 –  DeeMac Mar 23 '12 at 14:13
add comment
var diff = songs.Except(attributes.Select(a => a.name)).ToList();
share|improve this answer
add comment

This is the way to find all the songs which aren't included in attributes names:

var result = songs
  .Where(!attributes.Select(a => a.name).ToList().Contains(song));

The answer using Except is also perfect and probably more efficient.

EDIT: This sintax has one advantage if you're using it in LINQ to SQL: it translates into a NOT IN SQL predicate. Except is not translated to anything in SQL. So, in that context, all the records would be recovered from the database and excepted on the app side. Is much less efficiente.

share|improve this answer
Why did you post this if the other answers that use Except are perfect and more efficient? –  DeeMac Mar 23 '12 at 14:15
If you see it, I posted this before the other answers, and added the comment later, stating clearly that the other solutions are better. –  JotaBe Mar 23 '12 at 14:17
"I posted this before the other answers." You didn't. –  DeeMac Mar 23 '12 at 14:25
Plus, you're missing a right parenthesis. –  DeeMac Mar 23 '12 at 14:34
Hey, the voting system is here so that people knows which is the best answer. That's why I've voted adrian's. However I think it won't hurt to see a different alternative. Mine have at least one advantage. See my edited answer. –  JotaBe Mar 23 '12 at 15:24
show 4 more comments

It's worth pointing out that the answers posted here will return a list of songs not present in attributes.names, but it won't give you a list of attributes.names not present in songs.

While this is what the OP wanted, the title may be a little misleading, especially if (like me) you came here looking for a way to check whether the contents of two lists differ. If this is what you want, you can use the following:-

var differences = new HashSet(songs);
differences.SymmetricExceptWith(attributes.Select(a => a.name));
if (differences.Any())
    // The lists differ.
share|improve this answer
add comment

Your Answer


