如何从c#linq查询

时间:2015-09-28 10:33:08

标签: c# asp.net linq

我在这里堆叠我需要有人帮忙。我想从Refno等于Refno的不同表中获取最大日期。 我当前的代码给了我actionrecords表中存在的所有日期。

我的当前代码如

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

我只想在每个Refno中显示max(Transaction)。

请帮帮我们。

2 个答案:

答案 0 :(得分:1)

您需要在此使用 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) }); 并使用Max扩展方法查找最长交易日期: -

false

此外,作为旁注,因为String.IsNullOrEmpty返回布尔值,无需将其与trueif (!string.IsNullOrEmpty(refNo)) query = query.Where(x => x.RefNo.Contains(refNo.Trim())); 进行比较。你可以直接这样做: -

update tb_empgroc_master
JOIN tb_empgrocery on tb_empgroc_master.order_no = tb_empgrocery.order_no
set tb_empgroc_master.total_items='$total_items', 
    tb_empgroc_master.total_amount='$total_amount',
    tb_empgrocery.qty_ordered='$qty_ordered', 
    tb_empgrocery.amount='$amount'
where tb_empgroc_master.empgrocmstID='$empgrocmstID'
OR tb_empgrocery.empgrocID='$empgrocID'

答案 1 :(得分:0)

您可以在return语句之前添加以下行:

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

另一种选择是使用MoreLinq中的Max by属性函数,这将使代码更加优雅。

另外,你应该写:

 if (!string.IsNullOrEmpty(refNo))