Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a datatable retrieving from mysql.im my datatable i have column named penalty_date
and i have n number of columns.
i have to filter the minimum penalty_date from the datatable.
please give me linq query for this.
i have used this query
var lowest = System.Convert.ToDecimal(dtCollegeDetails.Compute("MIN(penalty_date)", "penalty_date > 0"));

but its not working fine.

plese send me any query
Posted
Updated 30-Dec-13 23:10pm
v2

You can try and check following linq statement.

var lowest = (from ds in dataRows select ds.Date).Min();

It should be like this to find MinDate.
 
Share this answer
 
Comments
Karthik_Mahalingam 31-Dec-13 5:51am    
5,gandalf, you r rite...
Gandalf_TheWhite 31-Dec-13 5:55am    
Glad it worked.
Try this..

C#
DateTime minimunDate =  dtCollegeDetails.Rows.OfType<datarow>().Select(k => Convert.ToDateTime(k["penalty_date"])).OrderBy(k => k).First(); 


or

C#
DateTime minimunDate = dtCollegeDetails.Rows.OfType<DataRow>().Select(k => Convert.ToDateTime(k["penalty_date"])).Min();
 
Share this answer
 
v2
Comments
Member 9700867 31-Dec-13 5:27am    
thanks karthick
Karthik_Mahalingam 31-Dec-13 5:32am    
welcome :)
use the below program
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Date
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("PaymentDate", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("Payment", typeof(int)));

            DataRow dr = dt.NewRow();
            dr["PaymentDate"] = new DateTime(2012,10,12);
            dr["Payment"] = 1200;
            dt.Rows.Add(dr);

             dr = dt.NewRow();
             dr["PaymentDate"] = new DateTime(2013, 10, 24);
            dr["Payment"] = 1200;
            dt.Rows.Add(dr);

             dr = dt.NewRow();
             dr["PaymentDate"] = new DateTime(2013, 11, 23);
            dr["Payment"] = 1200;
            dt.Rows.Add(dr);
//Method 1 using dataview
            DataView dv = dt.DefaultView;
            dv.RowFilter = "PaymentDate=Min(PaymentDate)";
            
            Console.WriteLine(string.Format("The Payment is {0} on {1}", dv[0][1], dv[0][0]));

// Method 2 using Linq
            DateTime drLatesPayment = (DateTime)dt.Rows.OfType<datarow>().Min( r => r["PaymentDate"]);

            Console.WriteLine(string.Format("The Payment is on {0}", drLatesPayment));    
            Console.Read();
        }
    }


}
 
Share this answer
 
v4
Comments
Karthik_Mahalingam 31-Dec-13 5:53am    
Hi Sarvesh,
whenever u r posting an answer , pls add it in the code block
it will be easy for the OP to review it..
Good luck..
Thanks
karthik.
Tom Marvolo Riddle 31-Dec-13 5:54am    
yes,you are right karthik :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900