2015-09-28 130 views
2

我在這裏堆棧我需要某人的幫助。我想從Refno相同Refno的不同表中獲取最大日期。 我現在的代碼給了我actionrecords表中存在的所有日期。如何從c#linq查詢表中選擇最大日期

我當前的代碼看起來像

public IQueryable<EmployerDetails> _employerProfileOverview(string refNo, string organisationname, string tradingname) 
{ 
    var query = (from _employerdetails in _entities.Organisations 
       join _lasttransaction in _entities.LevyTransactions on _employerdetails.Refno equals _lasttransaction.Refno 
       select new EmployerDetails 
       { 
        RefNo = _employerdetails.Refno, 
        Surname = _employerdetails.Surname, 
        TradingName = _employerdetails.TradingName, 
        Status = _employerdetails.Status, 
        TransactionDate = _lasttransaction.TransactionDate 
       }); 
    if (string.IsNullOrEmpty(refNo).Equals(false)) 
     query = query.Where(x => x.RefNo.Contains(refNo.Trim())); 

    if (string.IsNullOrEmpty(organisationname).Equals(false)) 
     query = query.Where(x => x.Surname.Contains(organisationname.Trim())); 

    if (string.IsNullOrEmpty(tradingname).Equals(false)) 
     query = query.Where(x => x.TradingName.Contains(tradingname.Trim())); 

    return query; 
} 

而我出來就把這個樣子

Refno  TradingName     Status TransactionDate 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2011-01-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2012-03-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2012-04-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2012-05-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2012-06-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2012-07-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2012-08-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2012-09-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2012-10-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2012-11-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2012-12-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2013-01-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2013-02-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2013-03-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2013-04-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2013-05-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2013-05-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2013-06-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2013-11-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2013-12-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-01-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-02-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-03-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-04-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-05-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-06-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-07-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-08-01 00:00:00.00 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-09-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-10-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-11-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2014-12-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2015-01-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2015-02-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2015-03-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2015-04-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2015-05-01 00:00:00.000 
L940732953 PSG KONSULT BPK LICHTENBURG  A 2015-06-01 00:00:00.000 
L940733035 BROUGHTON AND JONCK    A 2011-05-01 00:00:00.000 
L940733035 BROUGHTON AND JONCK    A 2011-05-01 00:00:00.000 
L940733035 BROUGHTON AND JONCK    A 2011-05-01 00:00:00.000 
L940733035 BROUGHTON AND JONCK    A 2011-05-01 00:00:00.000 
L940733035 BROUGHTON AND JONCK    A 2011-05-01 00:00:00.000 
L940733035 BROUGHTON AND JONCK    A 2011-05-01 00:00:00.000 
L940736574 COVERS FOODS CC     Y 2011-03-01 00:00:00.000 
L940736574 COVERS FOODS CC     Y 2011-03-01 00:00:00.000 
L940736574 COVERS FOODS CC     Y 2011-05-01 00:00:00.000 
L940736574 COVERS FOODS CC     Y 2011-05-01 00:00:00.000 
L940738141 TEAMWORX SHOPFITTERS (PTY) LTD A 2011-01-01 00:00:00.000 
L940738141 TEAMWORX SHOPFITTERS (PTY) LTD A 2011-02-01 00:00:00.000 
L940738141 TEAMWORX SHOPFITTERS (PTY) LTD A 2011-02-01 00:00:00.000 
L940738141 TEAMWORX SHOPFITTERS (PTY) LTD A 2011-04-01 00:00:00.000 
L940738141 TEAMWORX SHOPFITTERS (PTY) LTD A 2011-05-01 00:00:00.000 

我只是想顯示最大(交易)僅在每次每Personal Data個人紀錄。

請幫幫我吧。

回答

0

您可以直接return語句之前添加以下行:

query = query.OrderByDescending(q => q.TransactionDate).First(); 

另一種選擇是由物業功能MoreLinq使用最大,這將使得代碼更優雅。

此外,你應該寫:

if (!string.IsNullOrEmpty(refNo)) 
1

這裏需要使用group by和使用Max擴展方法找到最大的交易日: -

var query = (from _employerdetails in _entities.Organisations 
       join _lasttransaction in _entities.LevyTransactions 
       on _employerdetails.Refno equals _lasttransaction.Refno 
       group new { _employerdetails, _lasttransaction } 
       by new { _employerdetails.Refno, 
         _employerdetails.Surname_employerdetails.TradingName, 
         _employerdetails.Status } into g 
       select new EmployerDetails 
       { 
        RefNo = g.Key.RefNo , 
        Surname = g.Key.Surname, 
        TradingName = g.Key.TradingName, 
        Status = g.Key.Status, 
        TransactionDate = g.Max(x => x._lasttransaction .TransactionDate) 
       }); 

此外,作爲一個側面說明,因爲String.IsNullOrEmpty返回布爾值,不需要將其與falsetrue進行比較。您可以直接執行此操作: -

if (!string.IsNullOrEmpty(refNo)) 
     query = query.Where(x => x.RefNo.Contains(refNo.Trim())); 
相關問題