2013-04-02 65 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 ==月