get elements from list based on another list


I got two classes, like:

public class Person { public long Id { get; set; } public string Name { get; set; } } public class Vampire { public long Id { get; set; } }

Then, I have two lists, a list of persons and a list of vampires. All vampires are persons.

What I need is two children lists of persons, infected and notInfected. I'm building the two lists with a for, but I know it's possible using linq or something.

Any help?


Something like this:

var vampireIds = new HashSet<long>(vampireList.Select(x => x.Id)); var infectedPersons = personList.Where(x => vampireIds.Contains(x.Id)); var regularPersons = personList.Where(x => !vampireIds.Contains(x.Id));


I would go with something like the following:

void Main() { var list = new List<Person>(){ new Person(){ Id = 1 }, new Vampire(){ Id = 2 } }; var infected = list.Where (x => x is Vampire); var notInfected = list.Except(infected); } public class Person { public long Id { get; set; } public string Name { get; set; } } public class Vampire : Person { }


If only a person can be a Vapire, you could inherit Vampire from Person and then iterate through all persons and see if they are Vampires; if yes -> add to Vampire list, otherwise to non-Vampire list.


Try this:

var people = new List<Person> { new Person {Id = 1, Name = "John"}, new Person {Name = "Dave", Id = 2}, new Person {Id = 3, Name = "Sarah"} }; var vamps = new List<Vampire> {new Vampire {Id = 1}}; var theInfected = people.Where(p => vamps.Select(v => v.Id).Contains(p.Id)); var theAfraid = people.Except(theInfected); foreach (var person in theInfected) { System.Console.WriteLine(person.Name + " Is Infected!"); } foreach (var person in theAfraid) { System.Console.WriteLine(person.Name + " Is Afraid!"); }

Hope it's helpful.


  • How can I see Python's __builtins__ source code?
  • PHP Handling Namespace with SimpleXML
  • Angular2 emit event up to the DOM tree
  • Firebase, only get new children
  • Many to Many in Linq using Dapper
  • Multiple Left Join LINQ-to-entities
  • Efficient algorithm to find additions and removals from 2 collections
  • Building Qt project for C++11 standard
  • How can I get the choice “H2” back in the H2 consol?
  • Exception creating JSON with LINQ
  • Basic many-to-many left join query
  • Switch to popup in python using selenium
  • Add dynamic data to line chart from mysql database with highcharts
  • Merging rows to columns
  • Linq Merge lists
  • How to disable all widgets inside Panel or inside Composite?
  • Scala multiline string placeholder
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • pyodbc doesn't report sql server error
  • Conversion from string “a” to type 'Boolean' is not valid
  • Array with custom indexes in Ionic2
  • Grails calculated field in SQL
  • Sort List of Strings By Version
  • Rails Find when some params will be blank
  • Set the selected item in dropdownlist in MVC3
  • jQuery .attr() and value
  • Highlight one bar in a series in highcharts?
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Getting last autonumber in access
  • How to check if every primary key value is being referenced as foreign key in another table
  • MySQL WHERE-condition in procedure ignored
  • Display Images one by one with next and previous functionality
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • How to delete a row from a dynamic generate table using jquery?
  • using HTMLImports.whenReady not working in chrome
  • How do you join a server to an Active Directory (domain)?
  • How does Linux kernel interrupt the application?
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection