Linq-to-Sql Group通过内连接选择min

时间:2014-07-29 13:35:11

标签: sql vb.net linq-to-sql

我正在尝试将此查询转换为linq到sql,最好使用VB.NET。如果您只知道C#,我很可能会转换它。

select
    ha.HouseholdID_in,
    MIN(t.TradeDate_dt) AS FirstTradeDate_dt
from dbo.ivwHouseholdAccounts ha
    inner join dbo.trans t
        on ha.accountid_in = t.accountid_in
group by ha.HouseholdID_in

任何人都可以帮我一把吗?

1 个答案:

答案 0 :(得分:1)

以下是C#中应该是什么样子的简单示例。希望它有所帮助。

using (var context = new ExamenEnLigneDataContext())
{
    Dictionary<int, DateTime> lastDates = 
           (from ha in context.ivwHouseholdAccounts
           join t in context.trans
           on ha.accountid_in equals t.accountid_in
           group ha by new {ha.HouseholdID_in} into grp
           select new KeyValuePair<int,DateTime>(
               grp.Key.HouseholdID_in,
               grp.Min(t => t.TradeDate_dt)
           ).ToDictionary(d => d.Key, d => d.Value);
}