Reputation: 1317
I have a List<Record>
and a List<int>
I'm trying to select a new List<Object>
by selecting records from the original list by a List<int>
List<Record> Records = ...
List<int> RecordIds = new List<int> { 2,4,10 }
Here are a couple failing attempts at the syntax
List<Record> filteredRecords = Records.Where(p => p.RecordId.Any(z => RecordIds.Contains(z))).ToList();
List<Record> filteredRecords = Records.Where(p => p.Any(z => p.RecordId.Contains(RecordIds))).ToList();
List<Record> filteredRecords = Records.Any(p => p.RecordId.Equals(RecordIds)).ToList();
I'm having trouble figuring out the exact syntax to achieve what I want. I know I can achieve this using other various methods, but I'm trying to figure out the solution using the linq lamba syntax
Upvotes: 0
Views: 92
Reputation: 13577
Assuming Record has a RecordId: int
field:
var filtered = Records.Where(p => RecordIds.Contains(p.RecordId)).ToList();
From the original list of records, you take those that have a RecordId that appears in RecordIds list.
Upvotes: 3