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...?
Ganesh STech Lead cum developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Something like this:
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q29095399
{
    class Program
    {
        static void Main(string[] args)
        {
            string subject = "english";
            var subjects = new List<string> { "PHYSICS=100", "MATHS=100", "ENGLISH=99", "CHEMISTRY=97", "BIOLOGY=96" };
            var matches = (from s in subjects 
                           where s.IndexOf(subject, StringComparison.OrdinalIgnoreCase) > -1
                           select new { ColumnName = s.Split('=')[0], ColumnValue = s.Split('=')[1] });

            foreach (var match in matches)
            {
                Console.WriteLine(match);
            }
            Console.ReadLine();
        }
    }
}

Open in new window

Which produces the following output:Capture.PNG
-saige-
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
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
0
Ganesh STech Lead cum developerAuthor Commented:
Thanks..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.