Reputation: 4375
Is there a easy way to do this using linq?
I want to check and return true or false, whether any string from list1 contains a string from array1. In the below example that would be string2value and i would want to return true.
Also similarly i want to check whether any array1 would contain a string from list1. That would be string1blah and so that would return true as well. Thanks!
List<string> list1 = new List<string>{"string1","string2value"};
string[] array1 = new string[2] {"string1blah","string2"};
I have a couple of versions, but it does not work all the time.
array1.ToList().Any(a=>list1.Contains(a))
list1.Any(l=>array1.ToList().Contains(l))
Upvotes: 2
Views: 1286
Reputation: 27009
You can also use linq query expressions if you want.
Give this code:
List<string> list1 = new List<string> { "string1", "string2value" };
string[] array1 = new string[2] { "string1blah", "string2" };
I want to check and return true or false, whether any string from list1 contains a string from array1. In the below example that would be string2value and i would want to return true.
You can do it like this:
var result = from l1 in list1
from a1 in array1
where l1.Contains(a1)
select l1; // Or select true if you want to just return true
Also similarly i want to check whether any array1 would contain a string from list1. That would be string1blah and so that would return true as well. Thanks!
You can modify the above to achieve it like this:
var result = from l1 in list1
from a1 in array1
where a1.Contains(l1)
select a1;
Upvotes: 1
Reputation: 10781
If you're wanting pretty linq, you can do:
static void Main(string[] args)
{
var list1 = new List<string> { "string1", "string2value" };
var array1 = new string[] { "string1blah", "string2" };
var result = from sl in list1
from sa in array1
where sl.Contains(sa) || sa.Contains(sl)
select new { sl, sa };
foreach (var x in result)
{
Console.WriteLine(x);
}
}
Which gives nice output
{ sl = string1, sa = string1blah }
{ sl = string2value, sa = string2 }
Upvotes: 1
Reputation: 1551
To return true or false (as you've mentioned in the OP Comments), you would modify the call to count the items contained, and if greater than 0, return true.
EDIT Thanks Dax Fohl
var list1 = new List<string> {"Test1", "Test2", "Test3", "Test4"};
var array1 = new string[] {"Test3", "Test4", "Test5", "Test6"};
var result = list1.Any(s => array1.Any(s.Contains));
Upvotes: 0
Reputation: 8921
The way your question is worded it is hard to understand what you want.
If you want all the strings in one list which are substrings of an element in the other list, then this should work:
var substrings = myList.Where(bigstring => myArray.Any(substring => bigstring.Contains(substring)));
If you just want to know whether such an element exists, then do this:
bool myCondition = myList.Any(bigstring => myArray.Any(substring => bigstring.Contains(substring)));
Upvotes: 1
Reputation: 39326
You can try this:
var result= list1.Where(s=>array1.Any(s1=>s.Contains(s1)));
Per each string in the list1
, you see if there is an element in array1
that is contained in s
string.
Upvotes: 8