locked
C# or LINQ - How to search matching record and display specified format. RRS feed

  • Question

  • User-1024101449 posted

    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

        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 
    
                }
    
    
            }
    
        }

    Wednesday, July 26, 2017 1:30 PM

Answers

  • User-1509636757 posted

    Result  = "No Data",100,300,"No Data","No Data"

    Please check out below example (however, there may be some better solution than this one):

    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" };
        var varResult = OUTFORMULA.Select(str => DIMVALUE.Any(str1 => (string.IsNullOrEmpty(str1) ? ("0=0") : str1).Split('=')[0].Equals(str)) ?
        DIMVALUE.Where(str2 => (string.IsNullOrEmpty(str2) ? ("0=0") : str2).Split('=')[0].Equals(str)).Select(str2 => str2.Split('=')[1]).FirstOrDefault()
        : "No Data").ToArray();
        result = string.Join(",", varResult);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 27, 2017 6:34 AM
  • User-1509636757 posted

    Shall i use below code or any other better solution for this.

    This can also be an another alternative. Here is a modified example to handle "No Data", with help of a boolean flag that will be set to false if any value found. Check out:

    protected void Button8_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" };
        List<string> rel = new List<string>();
        for (int i = 0; i < OUTFORMULA.Length; i++)
        {
            bool noData = true;
            for (int j = 0; j < DIMVALUE.Length; j++)
            {
                string[] str = DIMVALUE[j].Split('=').ToArray();
                if (OUTFORMULA[i] == str[0]) // Meeds to be handle "No Data" also here
                {
                    noData = false;
                    rel.Add(str[1]);
                    break;
                }
            }
            if (noData) rel.Add("No Data");
        }
        result = string.Join(",", rel.ToArray());
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 27, 2017 6:59 AM

All replies

  • User-1509636757 posted

    With LinQ:

    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" };
        var varResult = DIMVALUE.Where(str => OUTFORMULA.Any(str1 => str.Contains(str1))).Select(str => str.Split('=')[1]).ToArray();
        result = string.Join(",", varResult);
    }

    Wednesday, July 26, 2017 6:01 PM
  • User303363814 posted

    result = OUTFORMULA.Join(DIMVALUE.Select(d=>d.Split('=')), 
                             o => o, 
                             d => d[0], 
                             (o, d) => d[1])
                       .Aggregate((res, t) => $"{res}, {t}");

    (The kaushalparik... solution won't work if the name of one subject is a substring of another subject. For example, if you have a subject PHYSICS L and another PHYSICS LIG)

    Wednesday, July 26, 2017 11:58 PM
  • User-1024101449 posted

    Thanks for your reply.

    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

    Thursday, July 27, 2017 4:34 AM
  • User-1509636757 posted

    PaulTheSmith

    (The kaushalparik... solution won't work if the name of one subject is a substring of another subject. For example, if you have a subject PHYSICS L and another PHYSICS LIG)

    Aha, I got that what you mean. I thought that was kind of requirement/issue with the question. Of course, the example I posted can break into PHYSICS L and PHYSICS LIG trap. Thanks for pointing that out. Correction here:

    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" };
        var varResult = DIMVALUE.Where(str => OUTFORMULA.Any(str1 => (string.IsNullOrEmpty(str) ? ("0=0") : str).Split('=')[0].Equals(str1))).Select(str => str.Split('=')[1]).ToArray();
        result = string.Join(",", varResult);
    }

    Thursday, July 27, 2017 5:18 AM
  • User-1024101449 posted

    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"

    Thursday, July 27, 2017 5:38 AM
  • User-1024101449 posted

    I want to get other values also 

    Result  = "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

    Thursday, July 27, 2017 5:46 AM
  • User-1024101449 posted

    Are you getting my points.

    if not, pls. let me know...

    Thursday, July 27, 2017 6:21 AM
  • User-1509636757 posted

    Result  = "No Data",100,300,"No Data","No Data"

    Please check out below example (however, there may be some better solution than this one):

    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" };
        var varResult = OUTFORMULA.Select(str => DIMVALUE.Any(str1 => (string.IsNullOrEmpty(str1) ? ("0=0") : str1).Split('=')[0].Equals(str)) ?
        DIMVALUE.Where(str2 => (string.IsNullOrEmpty(str2) ? ("0=0") : str2).Split('=')[0].Equals(str)).Select(str2 => str2.Split('=')[1]).FirstOrDefault()
        : "No Data").ToArray();
        result = string.Join(",", varResult);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 27, 2017 6:34 AM
  • User-1024101449 posted

    Shall i use below code or any other better solution for this.

     List<string> rel = new List<string>();
                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]) // Meeds to be handle "No Data" also here
                        {
                            rel.Add(str[1]); 
                            break;
                        }
                    }
    
                }
                result = string.Join(",", rel.ToArray());

    Thursday, July 27, 2017 6:48 AM
  • User-1509636757 posted

    Shall i use below code or any other better solution for this.

    This can also be an another alternative. Here is a modified example to handle "No Data", with help of a boolean flag that will be set to false if any value found. Check out:

    protected void Button8_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" };
        List<string> rel = new List<string>();
        for (int i = 0; i < OUTFORMULA.Length; i++)
        {
            bool noData = true;
            for (int j = 0; j < DIMVALUE.Length; j++)
            {
                string[] str = DIMVALUE[j].Split('=').ToArray();
                if (OUTFORMULA[i] == str[0]) // Meeds to be handle "No Data" also here
                {
                    noData = false;
                    rel.Add(str[1]);
                    break;
                }
            }
            if (noData) rel.Add("No Data");
        }
        result = string.Join(",", rel.ToArray());
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 27, 2017 6:59 AM
  • User-1024101449 posted

    Excellent Pro........

    Thursday, July 27, 2017 8:44 AM