C
C#2y ago
miNd

LINQ List A (list of strings) "partial" has any member of list B (list of strings)

For instance: List A has "Car","Bicycle" List B has "Blue Car", "Red Baloon","Random stuff" I want it to return true since List B has "Car" in it
10 Replies
Angius
Angius2y ago
Combine .Any() with .Contains() .Any() on the list, .Contains() on the string as a predicate
miNd
miNdOP2y ago
if (a.Any(item1=> b.Any(item2=> item1.Contains(item2)))) Like this?
Angius
Angius2y ago
Does it work?
miNd
miNdOP2y ago
doesnt look like it does
mtreit
mtreit2y ago
Narrator: it didn't work You need to use Contains on the strings themselves In the second list
miNd
miNdOP2y ago
item1.Contains item1 is already a string isnt it?
mtreit
mtreit2y ago
Yes, but unless I misunderstood it sounds like you have the lookup inverted. You're asking if "Car" contains "Blue Car" I think "Car" does not contain "Blue Car" "Blue Car" contains "Car"
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
List<string> a = new()
{
"Car", "Bicycle"
};

List<string> b = new()
{
"Blue Car", "Red Baloon", "Random stuff"
};

bool success =
b.Any(s1 => a.Any(s2 => s1.Contains(s2)));

success
List<string> a = new()
{
"Car", "Bicycle"
};

List<string> b = new()
{
"Blue Car", "Red Baloon", "Random stuff"
};

bool success =
b.Any(s1 => a.Any(s2 => s1.Contains(s2)));

success
Result: bool
True
True
Compile: 593.183ms | Execution: 71.943ms | React with ❌ to remove this embed.
ero
ero2y ago
so basically what you already had, just invert the lists
miNd
miNdOP2y ago
Thanks!
Want results from more Discord servers?
Add your server