如何在c#中两个给定日期之间使用数据集从数据库中获取最近注册的记录

时间:2015-11-11 14:00:03

标签: c# asp.net linq dataset

如何使用c#中的数据集从数据库中获取最近注册的记录。我想在两个给定日期之间获取最近注册的数据并生成报告。

我的表在数据库上是这样的 See image -Table in the database

Id        Refno       TradingName                       Seta                   TransactionDate           SetaLevy    SetaAdmin     SetaInterest   SetaPenalty  TotalSetaCollected   SchemeYear

70112109  L760732281  GOLDMAN SACHS INTERNATIONAL       BANKING                2014-01-01 00:00:00.000   -15918,12   -2404,9       0              0            -18323,02            2013

70200624  L250734490  TROPICANA IMPORTERS & EXPORTERS   WHOLESALE AND RETAIL   2014-01-01 00:00:00.000   -2805,75    -423,89       0              0            -3229,64             2013

70239219  L760732281  GOLDMAN SACHS INTERNATIONAL       BANKING                2014-02-01 00:00:00.000   -178917,65  -27030,73     0              0            -205948,38           2013

70320888  L250734490  TROPICANA IMPORTERS & EXPORTERS   WHOLESALE AND RETAIL   2014-02-01 00:00:00.000   -2734,63    -413,15       0              0            -3147,78             2013

70368752  L760732281  GOLDMAN SACHS INTERNATIONAL       BANKING                2014-03-01 00:00:00.000   -68072,97   -10284,41     0              0            -78357,38            2013

70450502  L250734490  TROPICANA IMPORTERS & EXPORTERS   WHOLESALE AND RETAIL   2014-03-01 00:00:00.000   -2563,18    -387,24       -2,75          -295,04      -3248,21             2013

70575355  L250734490  TROPICANA IMPORTERS & EXPORTERS   WHOLESALE AND RETAIL   2014-04-01 00:00:00.000   -2671,44    -403,6        -0,72          -307,5       -3383,26             2013

70575014  L380706855  BONITO DISTRIBUTORS CC            WHOLESALE AND RETAIL   2014-04-01 00:00:00.000   -490,42     -74,09        -0,26          -56,75       -621,52              2013

70489231  L760732281  GOLDMAN SACHS INTERNATIONAL       BANKING                2014-04-01 00:00:00.000   -17112,97   -2585,41       0             0            -19698,38            2013

70972265  L380706855  BONITO DISTRIBUTORS CC            WHOLESALE AND RETAIL   2014-05-01 00:00:00.000   224,18      33,87          -0,59         -33,44       224,02               2013

70972585  L760732281  GOLDMAN SACHS INTERNATIONAL       BANKING                2014-05-01 00:00:00.000   -15697,31   -2371,54        0            0            -18068,85            2014

71071473  L250734490  TROPICANA IMPORTERS & EXPORTERS   WHOLESALE AND RETAIL   2014-05-01 00:00:00.000   -2727,84    -412,12         0            0            -3139,96             2014

70932675  L740753381  KOREAN JOURNAL                    PUBLIC SECTOR          2014-05-01 00:00:00.000   -152,68     -21,81          0            0            -174,49              2011

我目前的代码是这样的

public DataSet getRecentRegistrations(DateTime startMonth, DateTime endMonth, long setaCode)
{
   var query = (from _transaction in
   _entities.LevyTransactions
   join _seta in _entities.Setas on _transaction.SetaCode equals _seta.SetaCode

 join company_details in _entities.Organisations on _transaction.Refno equals company_details.Refno

 where(_transaction.TransactionDate >= startMonth || startMonth == null)

 && (_transaction.TransactionDate <= endMonth || endMonth == null)

 && (_transaction.SetaCode == setaCode || setaCode < 0)

 select new

   {
     SetaCode = _transaction.SetaCode,

     RefNo = company_details.Refno,
     TradingName = company_details.TradingName,              
     Seta = _transaction.SetaCode+" - "+_seta.Description,
     SetaLevy = _transaction.SetaLevy,
     SetaInterest = _transaction.SetaInterest,
     SetaAdmin = _transaction.SetaAdmin,

     SetaPenalty = _transaction.SetaPenalty,
     TotalSetaCollected = _transaction.TotalSetaCollected
   });

DataSet ds = new DataSet();
ds.Tables.Add(query.CopyToDataTable());ds.Tables[0].TableName = "Table1";  
ds.Tables.Add(headerTable("Transaction Month From : "

 +HelperService.formatDate("yyyy-MM-dd", startMonth) + " To :"

 + HelperService.formatDate("yyyy-MM-dd", endMonth),"Recently Registered Companies")); 

ds.Tables[1].TableName ="Table2";
var setaRows = (from DataRow dRow in ds.Tables[0].Rows
select new { SetaCode = dRow["SetaCode"], Seta = dRow["Seta"]}).Distinct();

ds.Tables.Add(setaRows.CopyToDataTable()); ds.Tables[2].TableName = "Table3";
    return ds;
    }

我目前输出的报告只是给了我在两个日期之间找到的所有记录,但需要输出的只是新的注册记录。

所以我目前的出局是这样的。

最近注册日期: 2014-04-01 -2014-05-01

Id        Refno       TradingName                       Seta                   TransactionDate           SetaLevy    SetaAdmin     SetaInterest   SetaPenalty  TotalSetaCollected   SchemeYear

70575355  L250734490  TROPICANA IMPORTERS & EXPORTERS   WHOLESALE AND RETAIL   2014-04-01 00:00:00.000   -2671,44    -403,6        -0,72          -307,5       -3383,26             2013

70575014  L380706855  BONITO DISTRIBUTORS CC            WHOLESALE AND RETAIL   2014-04-01 00:00:00.000   -490,42     -74,09        -0,26          -56,75       -621,52              2013

70489231  L760732281  GOLDMAN SACHS INTERNATIONAL       BANKING                2014-04-01 00:00:00.000   -17112,97   -2585,41       0             0            -19698,38            2013

70972265  L380706855  BONITO DISTRIBUTORS CC            WHOLESALE AND RETAIL   2014-05-01 00:00:00.000   224,18      33,87          -0,59         -33,44       224,02               2013

70972585  L760732281  GOLDMAN SACHS INTERNATIONAL       BANKING                2014-05-01 00:00:00.000   -15697,31   -2371,54        0            0            -18068,85            2014

71071473  L250734490  TROPICANA IMPORTERS & EXPORTERS   WHOLESALE AND RETAIL   2014-05-01 00:00:00.000   -2727,84    -412,12         0            0            -3139,96             2014

70932675  L740753381  KOREAN JOURNAL                    PUBLIC SECTOR          2014-05-01 00:00:00.000   -152,68     -21,81          0            0            -174,49              2011

出局假设如下图所示

最近注册日期: 2014-04-01 -2014-05-01

Id          Refno       TradingName              Seta                  TransactionDate           SetaLevy   SetaAdmin   SetaInterest    SetaPenalty     TotalSetaCollected   SchemeYear
70575014    L380706855  BONITO DISTRIBUTORS CC   WHOLESALE AND RETAIL  2014-04-01 00:00:00.000   -490,42    -74,09      -0,26           -56,75          -621,52              2013
70932675    L740753381  KOREAN JOURNAL           PUBLIC SECTOR         2014-05-01 00:00:00.000   -152,68    -21,81      0               0               -174,49              2011   

See image -Output

请帮帮我。

0 个答案:

没有答案