Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ - Compare List objects and List of strings

Tags:

c#

.net

list

linq

I have a list of objects (List1) and list of string (List2 - list of Names of the objects)

I need to get all objects from List1 if the object.Name does not exists in List2

How can write this LINQ C#.?

like image 545
Sahi Avatar asked Feb 09 '17 07:02

Sahi


1 Answers

public class Class1
{
    public string Name {get;set;}
}

var List1 = new List<Class1>();
var List2 = new List<string>();
var result = List1.Where(x=>!List2.Contains(x.Name)).ToList();

Or:

var result = List1.Where(x=>!List2.Any(n=>n==x.Name)).ToList();
like image 142
Maksim Simkin Avatar answered Nov 20 '22 17:11

Maksim Simkin