locked
C# - How to Filter the row within the row in the range of Minimum and Maximum value RRS feed

  • Question

  • User-1024101449 posted

    Hi,

    I want to filter row within the row in the range of Minimum and Maximum value

    I done some code using LINQ, but i want simplified code either in C# or LINQ

     protected void Button_Click(object sender, EventArgs e)
        {

            
            string string1 = "SUB C=100#SUB D=200";

            string FinalOutput = "GRADE#SUB B";

            DataTable dt = new DataTable();
            dt.Columns.AddRange(new[] { new DataColumn("Studentno"),new DataColumn("GRADE"),new DataColumn("SUB B"), new DataColumn("SUB C(MIN)"), new DataColumn("SUB C(MAX)"),
    new DataColumn("SUB D(MIN)") ,new DataColumn("SUB D(MAX)") ,new DataColumn("SUB C"),new DataColumn("FROM"),new DataColumn("TO") });
            dt.Rows.Add(101,"A", "100", "100", "50", "200", "250", "700", 20, "100");
            dt.Rows.Add(101, "B", "100", "150", "70", "250", "200", "100", 20, "100");
            dt.Rows.Add(101, "B", "100", "200", "300", "100", "250", null, 20, "100");
            dt.Rows.Add(101, null, "100", "200", "300", "100", "200", null, 20, "100");



       I will explain step by step.


    1. I have two input strings.
        1-  string1 = "GRADE#SUB B";
        2-  string string2 = "SUB C=100#SUB D=300";

    2. I have the following conditions.
        First i want to get the row which contains in the range between within 100 ((i.e) SUB C (MIN) and SUB C (MAX)) (take string2 and split the string and take first in that. SUB C=100).

       so i may get below two records. (for condition SUB C=100)
      
               dt.Rows.Add(101,"A", "100", "100", "50", "200", "250", "700", 20, "100");
               dt.Rows.Add(101, "B", "100", "150", "70", "250", "200", "100", 20, "100");

    3. After that, with in the above two records, i want to filter one more condition need to check. (i.e : for SUB D=300
        This is also need to check within Min and Max of SUB D=300.

        So finally ia may get only one records.

              dt.Rows.Add(101,"A", "100", "100", "50", "200", "250", "700", 20, "100");

    4. In that, finally i want to get the records for the field of ( GRADE and SUB B).

    So the final output would be

    GRADE=A
    SUB B=100.

    Note : Filter and check row within the row using string2 and display the result in the form of string1.

    pls. let us know if you need any more details.

    The output i want either simple C# or Linq Query..???

    Monday, October 9, 2017 1:13 PM

Answers

  • User1771544211 posted

    Hi gani7787,

    Please take the following code as reference.

                string string1 = "GRADE#SUB B";
                string string2 = "SUB C=100#SUB D(MIN)=250";
    
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new[] { new DataColumn("Studentno"),new DataColumn("GRADE"),new DataColumn("SUB B"), new DataColumn("SUB C(MIN)"), new DataColumn("SUB C(MAX)"),
    new DataColumn("SUB D(MIN)") ,new DataColumn("SUB D(MAX)") ,new DataColumn("SUB C"),new DataColumn("FROM"),new DataColumn("TO") });
                dt.Rows.Add(101, "A", "100", "100", "50", "200", "250", "700", 20, "100");
                dt.Rows.Add(101, "B", "100", "150", "70", "250", "200", "100", 20, "100");
                dt.Rows.Add(101, "B", "100", "200", "300", "100", "250", null, 20, "100");
                dt.Rows.Add(101, null, "100", "200", "300", "100", "200", null, 20, "100");
                DataView dataView = dt.DefaultView;
    
                //Split the conidtion string and format the filter expression
                string[] condition = string2.Split('#');
                string filter = "";
                foreach (string subcondition in condition)
                {
                    if (!String.IsNullOrEmpty(filter))
                    {
                        filter += " AND ";
                    }
                    string[] subconditions = subcondition.Split('=');
                    filter += "[" + subconditions[0] + "] = '" + subconditions[1] + "'"; 
                }
    
                //filter the datatable
                var result = dt.Select(filter);
    
                //split the column names
                string[] fields = string1.Split('#');
                //get the column value from the filter result
                string Grade = result.FirstOrDefault().Field<string>(fields[0]);
                string SUBB = result.FirstOrDefault().Field<string>(fields[1]);

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 10, 2017 9:29 AM

All replies

  • User1771544211 posted

    Hi gani7787,

    Please take the following code as reference.

                string string1 = "GRADE#SUB B";
                string string2 = "SUB C=100#SUB D(MIN)=250";
    
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new[] { new DataColumn("Studentno"),new DataColumn("GRADE"),new DataColumn("SUB B"), new DataColumn("SUB C(MIN)"), new DataColumn("SUB C(MAX)"),
    new DataColumn("SUB D(MIN)") ,new DataColumn("SUB D(MAX)") ,new DataColumn("SUB C"),new DataColumn("FROM"),new DataColumn("TO") });
                dt.Rows.Add(101, "A", "100", "100", "50", "200", "250", "700", 20, "100");
                dt.Rows.Add(101, "B", "100", "150", "70", "250", "200", "100", 20, "100");
                dt.Rows.Add(101, "B", "100", "200", "300", "100", "250", null, 20, "100");
                dt.Rows.Add(101, null, "100", "200", "300", "100", "200", null, 20, "100");
                DataView dataView = dt.DefaultView;
    
                //Split the conidtion string and format the filter expression
                string[] condition = string2.Split('#');
                string filter = "";
                foreach (string subcondition in condition)
                {
                    if (!String.IsNullOrEmpty(filter))
                    {
                        filter += " AND ";
                    }
                    string[] subconditions = subcondition.Split('=');
                    filter += "[" + subconditions[0] + "] = '" + subconditions[1] + "'"; 
                }
    
                //filter the datatable
                var result = dt.Select(filter);
    
                //split the column names
                string[] fields = string1.Split('#');
                //get the column value from the filter result
                string Grade = result.FirstOrDefault().Field<string>(fields[0]);
                string SUBB = result.FirstOrDefault().Field<string>(fields[1]);

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 10, 2017 9:29 AM
  • User-1024101449 posted

    Thanks for your code.

    but, i need some more condition which we need to apply.


    string string2 = "SUB C=100#SUB D=250#SUB E=300",etc....; // This is also dynamic.some times it will come 3 paramter.sometimes 4 and sometimes 5,etc(now it is 3 parameter).

    i am struggling to match these cases in your code.

    as per above example, my output will be

    GRADE : A

    SUB B : 100

    can you give and some alternative idea about this...?

    Sample datatable as below..

    DataTable dt = new DataTable();
    dt.Columns.AddRange(new[] { new DataColumn("Studentno"),new DataColumn("GRADE"),new DataColumn("SUB B"), new DataColumn("SUB C(MIN)"), new DataColumn("SUB C(MAX)"),
    new DataColumn("SUB D(MIN)") ,new DataColumn("SUB D(MAX)") ,new DataColumn("SUB C"),new DataColumn("FROM"),new DataColumn("TO"),new DataColumn("SUB E(MIN)"), new DataColumn("SUB E(MAX)") });
    dt.Rows.Add(101, "A", "100", "80", "200", "200", "550", "700", 20, "100","200","500");
    dt.Rows.Add(101, "B", "100", "90", "300", "270", "200", "100", 20, "100",null,null);
    dt.Rows.Add(101, "B", "100", "200", "300", "100", "250", null, 20, "100","100","100");
    dt.Rows.Add(101, null, "100", "200", "300", "100", "200", null, 20, "100",null,null);

    Tuesday, October 10, 2017 9:47 AM
  • User-1024101449 posted

    Any update..this scenario is little bit tricky and finding the exact solution...

    Wednesday, October 11, 2017 4:19 AM
  • User1771544211 posted

    Hi  gani7787,

    string string2 = "SUB C=100#SUB D=250#SUB E=300",etc....; // This is also dynamic.some times it will come 3 paramter.sometimes 4 and sometimes 5,etc(now it is 3 parameter).

    Your datatable doesn't have a column which name is "SUB D" or "SUB E", you only have columns SUB D(MIN), SUB D(MAX), SUB E(MIN) and SUB E(MAX). When you try my above code, please use the exact column name.

    My above code should be able to handler multiple parameters, have you tried my code, what's the result?

    Best Regards,

    Jean

    Wednesday, October 11, 2017 5:32 AM
  • User-1024101449 posted

    Yes. i debugg and found the solution.

    we need to enclose '(single quotes) before and after of A1.

    Thanks buddy...

    It's working now..

    Note : If you have any alternate solution pls. provide us. we will keep in mind...

    Wednesday, October 11, 2017 6:27 AM