C
C#17mo ago
mindzor_

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
Angius17mo ago
Combine .Any() with .Contains() .Any() on the list, .Contains() on the string as a predicate
mindzor_
mindzor_17mo ago
if (a.Any(item1=> b.Any(item2=> item1.Contains(item2)))) Like this?
Angius
Angius17mo ago
Does it work?
mindzor_
mindzor_17mo ago
doesnt look like it does
mtreit
mtreit17mo ago
Narrator: it didn't work You need to use Contains on the strings themselves In the second list
mindzor_
mindzor_17mo ago
item1.Contains item1 is already a string isnt it?
mtreit
mtreit17mo 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
MODiX17mo 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
ero17mo ago
so basically what you already had, just invert the lists
mindzor_
mindzor_17mo ago
Thanks!
Want results from more Discord servers?
Add your server
More Posts