Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List and array comparison

I have a requirement wherein I have a value in Viewbag.Keyword like "Johnson and Jen" so I have to split this value and compare it with List.

I was trying the below code but facing error some or the other way(eg; this is char not string and so on) and it doesn't look like smart coding as per latest functionalities available in framework 4.5

var str = Convert.ToString(ViewBag.Keyword);
string[] str1 = str.Split(' ');

for (int j = 0; j < str1.Length;j++)
{
    string str2 = Convert.ToString(str[j]);
    if (result.Document.ContactName.IndexOf(str2, 
        StringComparison.CurrentCultureIgnoreCase) != -1)

Note: "ContactName" is string. Please help with some latest code which improves performance.

like image 783
harshu288 Avatar asked May 25 '26 08:05

harshu288


1 Answers

You can use linq to check if result.Document.Name contains a word from array.

var str = Convert.ToString(ViewBag.Keyword);
string[] str1 = str.Split(' ');
var names = str1.Where(x => (result.Document.ContactName.Contains(x))).ToList();
like image 180
Mairaj Ahmad Avatar answered May 27 '26 21:05

Mairaj Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!