Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

LINQ - How to write LINQ query to split and find the column min and maximum and return based on string values.

$
0
0

Hi,

How to write LINQ query to split and find the column min and maximum and return based on string values.

I done some code using LINQ, but it is not working.

what is the problem in my code.

Scenario is,

1. Split the string and find the column

2. Check the minimum and maximum range of each splitted string. (SUB C=100#SUB D=200)

3 range should check within the limit like 100 min and max

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", "200", "200", "300", null, 20, "100");
        dt.Rows.Add(101, "B", "100", "150", "250", "250", "350", null, 20, "100");
        dt.Rows.Add(101, "B", "100", "200", "300", "100", null, null, 20, "100");
        dt.Rows.Add(101, null, "100", "200", "300", "100", null, null, 20, "100");



        //Check whether a given column name exists or not


        string[] word1 = string1;

        List<string> FinalResult = new List<string>

        for (int i = 0; i < word1.Length; i++)
        {

            string ChkDim = word1[i];

            var results = (from row in dt.AsEnumerable()
                           from pair in
                               (from term in ChkDim.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                let pair = term.Split(new[] { '=' })
                                where pair.Count() == 2 && int.TryParse(pair[1], out dimens)
                                select new KeyValuePair<string, int>(pair[0], dimens))
                           where row[pair.Key + "(MIN)"] != DBNull.Value && row[pair.Key + "(MAX)"] != DBNull.Value
                           let r1 = Convert.ToInt64(row[pair.Key + "(MIN)"])
                           let r2 = Convert.ToInt64(row[pair.Key + "(MAX)"])
                           let FrSize = Convert.ToDouble(row.Field<double>("FROM SIZE"))
                           let ToSize = Convert.ToDouble(row.Field<double>("TO SIZE"))
                           where pair.Value >= r1 && pair.Value <= r2 &&

                           PipeDia >= FrSize && PipeDia <= ToSize
                           select new
                           {

                                FinalValue = row[column] // Need to get column value. Output is GRADE=A and SUB B=100

                           }).Distinct();


            FinalResult.Add(word1[i] + "=" + FinalValue);

        }

        //finally Result





    }

 

My final output should return in in the List.
[0] GRADE=A
[1] SUB B=100

what is the problem in my code...


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>