Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have two list collections. One with keys and one with records.

class Record
{
int key;
string Data;
}

List<int> Keys = new List<int>{1,2,3};

List<record> records = [SOME_RECORDS]
</record></int></int>


How to get all items from Records list which key property is in Keys list?
Could it be one line linq expression?
Posted
Updated 10-Apr-11 23:59pm
v2

Using your code, I'd do it like this:

C#
var results = records.Where(r => Keys.Contains(r.key));
 
Share this answer
 
Comments
Kim Togo 11-Apr-11 7:11am    
Agree, the power of LINQ :-)
Umesh AP 15-Mar-16 6:31am    
Gr8
Or you can also do this with a join in case you want to add more conditions etc:
var query = from record in records
            join key in Keys on record.key equals key
            select record;
In case you can have multiple keys for the same record, you can use the Distinct operation
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900