我正在使用Linq2Sql管道/过滤器。我的数据库由帐户和交易表组成,其中每个交易行都链接到一个帐户。内部列表对象上的Linq2Sql管道和过滤器
显示帐户时,我想显示帐户的所有交易。够简单。现在我试图限制以/ accounts/4/{year}/{month}/{day}为例的交易。
Account a = AccountsRepository.GetAccounts() .WithID(id) .FilterTransactions(year, month, day) .SingleOrDefault();
那么,我该如何编写一个过滤器来返回帐户,但也过滤返回的交易?
当我运行没有FilterTransactions探查我得到2 SQL调用...
exec sp_executesql N'SELECT [t0].[ID], [t0].[BankName], [t0].[BankCode], [t0].[CardNumber], [t0].[Locale] FROM [dbo].[Accounts] AS [t0] WHERE [t0].[ID] = @p0',N'@p0 int',@p0=1 exec sp_executesql N'SELECT [t0].[ID], [t0].[AccountID], [t0].[Date], [t0].[Description], [t0].[Amount] FROM [dbo].[Transactions] AS [t0] WHERE [t0].[AccountID] = @p0',N'@p0 int',@p0=1
我的想法是,在过滤器中做类似的信息(年度简单的例子)
public static IQueryable<Account> FilterTransactions( this IQueryable<Account> qry, int? year, int? month, int? day) { ...loop through each Account a.Transactions = a.Transactions .Where(t => t.Date.Year == year); }
但是,a.Transactions是一个EntitySet,Where返回一个IEnumerable。
另一种解决方案是将帐户返回到我的视图,然后在视图中调用HtmlHelper,该HtmlHelper调用另一个操作来显示事务。但是,这将导致1额外的SQL调用,并导致我显示我需要的信息的问题。