I have a class of type
public class One
{
public string firstString { get; set; }
public string secondString { get; set; }
}
And IEnumerable:
IEnumerable<One> firstIEnumerable;
During program execution I get another IEnumerable,
IEnumerable<string> secondIEnumerable = List<string>.Where(...);
What I want is to request firstIEnumerable
's secondString
using equality between firstIEnumerable
's firstString
member and any value from secondIEnumerable
.
I imagine a command like this:
IEnumerable<string> thirdIEnumerable =
firstIEnumerable.Where(m=>m.firstString == secondIEnumerable.? ? ? ? ?).secondString;
But it doesn't compile.
If the second enumerable is a long sequence you are probably (benchmark to check) better off doing two passes and creating a hash-set, rather than doing the O(N * M) algorithm. The O(N * M) one would be:
first.Where(x => second.Contains(x.firstString)).Select(x => x.secondString)
Using a hash set:
var hashSet = new HashSet<string>(second);
first.Where(x => hashSet.Contains(x.firstString)).Select(x => x.secondString)
The second option is likely to provide much better performance in most scenarios.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments