1

There are examples around for this in SQL and I understand them, but I can't seem to wrap my head around it in Linq-SQL.

There are two tables, 'Accounts' and 'AccountTransactions' related by the primary key in Accounts, which is, surprise, AccountID.

I need to select each Account row and the 1 Most Recent (Top 1 when ordered Descending) child AccountTransaction.

I've tried to hack at some examples I've found but no luck (just not getting it I guess)...

Any help appreciated...

4

2 に答える 2

1

これは1つの可能な解決策です。きれいではありませんが、機能するはずです。

from account in Accounts
select new
    {account.Name, account.Whatever,
    LastTransaction =
        account.AccountTransactions.OrderByDescending(t => t.Date).First()};

これにより、すべてのアカウントが選択され、AccountTransactionsテーブルの外部キーとしてAccountIDが含まれている場合、結合が自動的に実行されます。アカウントに必要な詳細を取得し、日付順に並べられた最新のトランザクションを取得するだけです。

于 2011-03-23T06:39:31.017 に答える
0
  1. tblLink リンク = ( c.LinkId == 3 && c.RegionId == 5 を選択した context.tblRegionLinks の c から c.tblLink).FirstOrDefault();

私の場合はうまくいきます.....

それが他の誰かを助けることを願っています。

于 2011-12-20T06:36:37.757 に答える