如何使用c#中的数据集从数据库中获取最近注册的数据?我想在两个给定日期之间获得最近的注册数据。
我的表在数据库中是这样的: Table in the database
我目前的代码是这样的:
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