Link to home
Start Free TrialLog in
Avatar of Gani tpt
Gani tpt

asked on

How to fetch the matching record from the list..?

string SpeSubject = "ENGLISH"
List<string> SubjectAll = new List<string>() { "PHYSICS=100", "MATHS=100", "ENGLISH=99", "CHEMISTRY=97", "BIOLOGY=96" };

string[] Result = SubjectAll.Where(str => SpeSubject.Any(str1 => str.Contains(str1))).ToArray();

How to fetch the Matching values from SubjectAll.

For Example, My searchstring is ==> SpeSubject = "English"

I want to search this field ("ENGLISH") in SubjectAll list and return as "ENGLISH=100"

i written the query below. But, it will fetch only the first records.

How to fetch the exact record..?



string SpeSubject = "ENGLISH"
List<string> SubjectAll = new List<string>() { "PHYSICS=100", "MATHS=100", "ENGLISH=99", "CHEMISTRY=97", "BIOLOGY=96" };

string[] Result = SubjectAll.Where(str => SpeSubject.Any(str1 => str.Contains(str1))).ToArray();

Finally i want to store English and Marks are seperate.


string ColumnName = Result.Select(str2 => str2.Split('=')[0]).FirstOrDefault();  

string ColumnValue = Result.Select(str2 => str2.Split('=')[1]).FirstOrDefault());

my final value should be as

ColumnName = ENGLISH
ColumnValue = 99

where we need to change the code...?
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you may consider to using a dictionary instead of a list:

        Dictionary<string, int> dictionary =
            new Dictionary<string, int>();

        dictionary.Add("PHYSICS", 100);
        dictionary.Add("MATHS", 100);
        dictionary.Add("ENGLISH", 99);
        dictionary.Add("CHEMISTRY", 97);
        dictionary.Add("BIOLOGY", 96);

        // See whether Dictionary contains this string.
        if (dictionary.ContainsKey("ENGLISH"))
        {
            int value = dictionary["ENGLISH"];
            Console.WriteLine(value);
        }

        // or alternatively 
        // See whether it has a key and get the value if so
        if (dictionary.TryGetValue("ENGLISH", out test))
        {
            Console.WriteLine(test);
        }
    }

Open in new window


Sara
Avatar of Gani tpt
Gani tpt

ASKER

Thanks..