Reputation: 193
I'm having trouble creating a list from an another list
original list:
key | fname | lname
------ | ------ | --------
11111 | hank | smith
1 | john | doe
22222 | jane | smith
2 | jim | smith
Here is my comma delimited list that I wanted to get the new list from
var search = "1,2";
what I'd like to have in my new list
key | fname | lname
------ | ------ | --------
1 | john | doe
2 | jim | smith
I could do a lambda if I was just looking for a single value. i.e.
var newList = originalList.firstOrDefault(x => x.key == "1")
how could I do this replacing the "1" with my search variable?
Upvotes: 2
Views: 2316
Reputation: 46229
Another way you can use lambda JOIN
search.Split(',')
let "1,2"
splite be string array, which contain the keys.
var search = "1,2";
var newList = originalList.Join(search.Split(','), p => p.key, s => s, (p, s) => p);
Upvotes: 0
Reputation: 19496
It will be easier if you use Split()
first to get a list of strings from your comma-separated list:
var searchList = search.Split(',');
Then you can use Where()
to filter:
var newList = originalList.Where(x => searchList.Contains(x.Key));
Upvotes: 5