ZZZSharePoint
ZZZSharePoint

Reputation: 1351

matching the value only with a subset of string

I have this value

public static Dictionary<string, string> projectCustomerNameMapping = new Dictionary<string, string>()
        {            
            {"Diooiio","Oppo" }
            {"SSoois","RealMe"}
        };

which I am reading like this

 if( !projectCustomerNameMapping.TryGetValue( (string)data["Project"], out customerName ) )
                    {
                        // if the value wasn't found in the dictionary, use the default
                        customerName = "OnePlus";
                    }

My question is above it actually matches the Project and then gives the customerName, but I would like if it contains a value from project it should return the customer name. so if my project has value as "PoC_Diooiio" or "Diooiio" or anything which contains "Diooiio" it should give me customer name "Oppo". How do I fix it?

Upvotes: 0

Views: 156

Answers (3)

Dorin Baba
Dorin Baba

Reputation: 1728

string search = data["Project"];
var customerName = projectCustomerNameMapping
        .FirstOrDefault(q => q.Key.IndexOf(search ) != -1 || search.IndexOf(q.Key) != -1)
        .Value ?? "OnePlus";

Upvotes: 1

Siva Makani
Siva Makani

Reputation: 216

string temp = "Diooiio";
if(customerName.contains(temp) == true)
{
   customerName = projectCustomerNameMapping[temp]; 
}

You may loop through all the elements of Dictionary for rest.

Upvotes: 0

James
James

Reputation: 11

Why don't you try using Linq. The below will return a Like resultset for you.

This is just a guide for your code. It will return the first response it finds, if there is one, and may not always be the one you want.

If it finds nothing, it will return "OPPO"


         public string findCustomerName(string input)
        {
            var result = projectCustomerNameMapping.FirstOrDefault(x => x.Key.Contains(input));
            if (result.Key == null)
                return "OPPO";
            else
                return result.Value;
        }

Upvotes: 1

Related Questions