16

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
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321

4 Answers4

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

edit

Added ToList() to make it a list

Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
  • An enumerable. Use ToList() if you need to. –  Mar 23 '12 at 14:13
  • 3
    Sorry Adrian, beat me to it. This is actually a really good answer, been wondering how to achieve this for a while myself. +1 –  Mar 23 '12 at 14:13
6

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.
}
Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
5

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, which is much less efficient.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • Why did you post this if the other answers that use `Except` are perfect and more efficient? –  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
  • 1
    "I posted this before the other answers." You didn't. –  Mar 23 '12 at 14:25
  • Yes I'm missing a right parentheses. But look at the post times: mine 27 minutes ago, edited 20 minutes ago; ionden 25 minutes ago; mine 24 minutes ago.But as I can see it's offending you, I 'll delete it. – JotaBe Mar 23 '12 at 14:37
  • The 'but' implies that because you posted it earlier (which you never) that you're exempt from missing a parenthesis and it qualifying as a suitable answer. You're wrong with those times. I can blatantly see you posted it later. Yes it offends me, delete it. –  Mar 23 '12 at 14:39
  • Even if you did post earlier, if another clearly superior and more correct answer is posted by someone else you should just delete your post. – Servy Mar 23 '12 at 15:01
  • No, "but" didn't imply that I had the right to not correct my answer . It meant that you was right regarding the parentehsis but not the posting time. Please, don't blame on me: oddly enough, I know see my answer was posted 32 min ago, ionden's 33 min ago, and Adrian's 34 minutes ago, so mine was last in the row. Can't understand it. And, althought I was going to do it, as Adrian corrected my answer I'm not deleting it. By the way I told Except is "probably" more efficient, but I'll have to give it a try to confirm it. – JotaBe Mar 23 '12 at 15:02
  • 7
    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
  • Wow, this got real serious. – markokstate Mar 15 '17 at 20:20
4
var diff = songs.Except(attributes.Select(a => a.name)).ToList();
ionden
  • 12,536
  • 1
  • 45
  • 37