2013-04-02 93 views
0

我知道如何获得交易数量,但是如何获得特定月份的计数?获取特定月份和年份内的交易数量

如果我想2013年3月的交易为例?

这里是我当前的查询:

textBox_PaymentsCount.Text = 
     (from tot in _entities.Payments 
     where tot.CorporationId == _currentcorp.CorporationId && 
     tot.Result != null 
      select tot).Count().ToString(CultureInfo.InvariantCulture); 

我需要由现场TransactionDateTime,以尽量减少我查询了。

我试着指定第一天和最后一天,但最后一天的月份有所不同。

有没有简单的方法来做到这一点?

回答

1

将另一个条件添加到where子句中并使用DateTime.MonthDateTime.Year属性:

tot.DateProperty.Month == 3 && tot.DateProperty.Year == 2013 

整个查询看起来应该:

textBox_PaymentsCount.Text = 
    (from tot in _entities.Payments 
    where tot.CorporationId == _currentcorp.CorporationId && 
    tot.Result != null && 
    tot.DateProperty.Month == 3 && tot.DateProperty.Year == 2013 
    select tot).Count().ToString(CultureInfo.InvariantCulture); 
+0

真好!我不知道这存在!我会试一试 – ErocM

2

但最后一天因月份而异。

你可以使用目前的下个月的第一天,并使用<下个月:

&& TransactionDateTime >= firstDayOfThisMonth 
&& TransactionDateTime < firstDayOfNextMonth 
2

我认为你正在寻找的东西是这样的:

where tot.TransactionDateTime.Year == year && tot.TransactionDateTime.Month == month 
0

尝试:

var from = new DateTime(2013, 1, 1); 
var until = new DateTime(2013, 2, 1); 

.... 
where tot.CorporationId == _currentcorp.CorporationId 
&& tot.Result != null 
&& tot.TransactionDateTime > from && tot.TransactionDateTime < until 
.... 
0

第一天I = 1 和最后一天= 30或31或29分之28 创建的结构连接月份对应于最后日期 和按月输入 它将采取相应的最后一天 ,然后将计算出no。给定月份的交易

0

刚刚得到的月份和年份从你TrasanctionDateTime

tot.TransationDate.Year ==年& & tot.Transaction.Month ==月

相关问题