本文介绍了如何使用linq从数据表列中检索最小日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有从mysql.im检索的数据表我的数据表我有列名为penalty_date

我有n列。

i必须从数据表中过滤最小的penalty_date。

请给我linq查询。

i使用了这个查询

var low = System.Convert.ToDecimal(dtCollegeDetails.Compute(MIN(penalty_date),penalty_date> 0));



但不是工作正常。



请给我任何查询

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

推荐答案


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











or

DateTime minimunDate = dtCollegeDetails.Rows.OfType<DataRow>().Select(k => Convert.ToDateTime(k["penalty_date"])).Min();


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();
        }
    }


}


这篇关于如何使用linq从数据表列中检索最小日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 18:50