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?
 
it_saigeConnect With a Mentor DeveloperCommented:
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
 
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
All Courses

From novice to tech pro — start learning today.