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

asked on

C# or LINQ - How to search matching record and display specified format

Hi,

I done some sample code which should search matching record and display in specified format.

i am unable to get exact format.

my output is : 100,300 (because only matching ENGLISH H and MATHS L)

see my sample code's
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Not enough information given and "What sample code"?
Avatar of Gani tpt
Gani tpt

ASKER

 protected void Button7_Click(object sender, EventArgs e)
    {

        string result = null;


        string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };

        string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };


        if (OUTFORMULA != null)
        {
            string re1 = null; 
            for (int i = 0; i < OUTFORMULA.Length; i++)
            {
                string re = null;
                for (int j = 0; j < DIMVALUE.Length; j++)
                {
                    string[] str = DIMVALUE[j].Split('=').ToArray();
                    if (OUTFORMULA[i] == str[0])
                    {
                        re1 = str[1];
                        break;
                    }

                }

                re = re + re1 + ",";

                result = string.Concat(result, re); // OutPut Should come only 100,300 

            }


        }

    }

Open in new window

Please give a detail description of what you want your code to do and what it is not doing. Are you getting exceptions or what?
Perhaps you could use a List.
string result = null;


            string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };

            string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };


            if (OUTFORMULA != null)
            {
                List<string> rel = new List<string>();
                
                for (int i = 0; i < OUTFORMULA.Length; i++)
                {
                    
                    for (int j = 0; j < DIMVALUE.Length; j++)
                    {
                        string[] str = DIMVALUE[j].Split('=').ToArray();
                        if (OUTFORMULA[i] == str[0])
                        {
                            rel.Add(str[1]);
                            break;
                        }

                    }

                }

                result = string.Join(",", rel.ToArray());

Open in new window

1. I am checking which available list in DIMVALUE .
2. If matching the record then we should store as a string separated by comma
For Example,

Sample example matching on ly two records (ENGLISH H and MATHS L).

3. we need to take only values of ENGLISH H and MATHS L and form the string like 100,300.

so final output is : 100,300
if I use list then it will come as index format.

I want simple string like 100,300.
This converts the List to a comma delimited string.
result = string.Join(",", rel.ToArray());

Open in new window

Excellent.Thanks.
Given your input the following code snippet will do what you need.
string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };

string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };

var query = (from d in DIMVALUE
             from o in OUTFORMULA
             where d.StartsWith(o)
             select d.Substring(d.IndexOf('=') + 1)).ToArray();

string result = query.Length == 0 ? "" : String.Join(",", query);

Open in new window

My solution:

      string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };

            string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };
            string result="";
            foreach (string s in DIMVALUE)
            {
                string[] pair = s.Split(new char[] { '=' });

                if (OUTFORMULA.Contains(pair[0]))
                    result += pair[1] + ",";
            }
            result = result.Trim(new char[] { ',' });

Open in new window

Is it possible to add one more conditions. let's say

for example,

if "TAMIL TAG" will not found in the DIMVALUE then give label "No Data".

so the final output,

No Data,100,300,No Data,No Data
I want to get the result which is mentioned in the OUTFORMULA.

string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };

Some times OUTFORMULA Value is missing in DIMVALUE. (For Ex : TAMIL TAG,"PHYSICS LIG", "SCIENCE GROUP")

string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };

So the user can understand if we mentioned "No data" for the particular value, it will be useful to refer the OUTFORMULA.


OUTPUT Result should idsplay in the order of OUTFORMULA.

string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };

Resutl = "No Data",100,300,"No Data","No Data"

The final intention is Result value count should always equal to OUTFORMULA Count

Result  = "No Data",100,300,"No Data","No Data" = ==> Count =5 and order by same

string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" }; ==> Count = 5 and order by same
Here's the code I posted adjusted to add 'No Data' when there's no match - I'm sure the other guys can come up with a more elegant solution.:)
            string[] OUTFORMULA = { "TAMIL TAG", "ENGLISH H", "MATHS L", "PHYSICS LIG", "SCIENCE GROUP" };

            string[] DIMVALUE = { "HISTORY X=500", "GEOGRAPHY K=900", "ENGLISH L1=100", "ENGLISH L2=1100", "ENGLISH H=100", "MATHS L=300" };

            var query = (from d in DIMVALUE
                         from o in OUTFORMULA
                         where d.StartsWith(o)
                         select d.Substring(d.IndexOf('=')+1)).ToArray();

            string result = query.Length == 0 ? "" : String.Join(",", query);

            if (OUTFORMULA != null)
            {
                List<string> rel = new List<string>();
                
                for (int i = 0; i < OUTFORMULA.Length; i++)
                {
                     bool find = false;
                    for (int j = 0; j < DIMVALUE.Length; j++)
                    {
                        string[] str = DIMVALUE[j].Split('=').ToArray();
                        if (OUTFORMULA[i] == str[0])
                        {
                            rel.Add(str[1]);
                            find = true;
                            break;
                        }

                    }

                    if  (!find) rel.Add("No Data");

                }

                result = string.Join(",", rel.ToArray());

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
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
SOLUTION
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
Oops, kind of included some of the other guys code in my last post - was testing it out as still new to using LINQ.:)
Thanks for your Excellent Solution using LINQ and For each loop..
If one or more solutions answered your question please close the question by awarding points.

Thanks
Closing as author acknowledged both solutions.