Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a List of Item named "FunctionList" and also have a string named "mainString"?

I want to find the matching item one by one from "FunctionList" which belongs to "mainString".

The output will be like

C#
var matchItem= "check each item into string belongs to string or not";  


Here matchItem will be the item from List belongs to string also. Please help me to find out the code for this solution using C# or LINQ.
Posted
Updated 29-Apr-15 4:42am
v2
Comments
CHill60 29-Apr-15 9:30am    
Is this the same as your question How can I split string with expected parenthesis ?[^]?
If not then this does not make it clear what your problem is.
If it is the same question then I've already answered it.
sachi Dash 30-Apr-15 1:03am    
This is another question. Thanks for the reply.
Sergey Alexandrovich Kryukov 29-Apr-15 10:43am    
Off-topic. Surprised? I'll explain: software engineering is NOT "finding out the code".
—SA

Ok - I think I get what you're asking

You have a List<item> FunctionList and a \s separated string of terms.

You want to find the matching FunctionList item for each term in the string.

I imagine that some part of the FunctionList is a string that matches the term?

If this is not correct then please comment and I will update or remove my answer!

C#
//first, break your terms into an array

string[] terms = mainString.split(' '); // you can get rid of double spaces before or parse for empty strings after if your mainString has dirty data

foreach(string term in terms)
{
    //if the FunctionList is a List<string>
    if(functionList.Contains(term)){}
    //but I doubt that it is

    //using linq:
    var items = functionList.Where(f=>f.Name==term); //guessing there's a string field - calling it name
    if(!items.Any()){continue; // No Match}
    else if(items.Count()>1) {break; //multiple matches}
    else{
        //Exactly 1 match
        Item item = items.Single();
          item.DoSomething();
    }

    //using a second loop
    foreach(Item item in FunctionList){
        if(item.Name = term)
        {
             item.DoSomething();
             //exit this loop after first match?
             break;
        }
        //You can handle multiple / no match issues if you need to
        //This is just a basic example
    }
}
 
Share this answer
 
v2
I'm not sure i understand you well, but... if you want to find matches, try this:
C#
List<string> FunctionList = new List<string>(){"bla","ble","bli","blo"};
string mainString = "bli";

var matches = FunctionList.Where(a=>a==mainString);
//or
//var matches = FunctionList.Where(a=>a.Contains(mainString));
foreach(var m in matches)
{
    Console.WriteLine("{0}", m);
}


For further information, please see:
Enumerable.Contains<tsource> Method (IEnumerable<tsource>, TSource)[^]
101 LINQ Samples[^]
 
Share this answer
 
v2
Comments
sachi Dash 30-Apr-15 6:06am    
You got it. Then how can I separate value from matches?
Maciej Los 30-Apr-15 6:08am    
You need to loop through the matches. See updated answer.
sachi Dash 30-Apr-15 6:32am    
Sorry it doesn't show anything ! I used this code.
foreach (var m in matches)
{
MessageBox.Show(m.ToString());
}
Maciej Los 30-Apr-15 6:41am    
???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900