我正在使用带有管道/过滤器的Linq2Sql。我的数据库由账户和交易表组成,其中每个交易行都链接到一个账户。
显示账户时,我想显示账户及其所有交易。很简单。现在,我试图限制例如/accounts/4/{年}/{月}/{日}显示的交易。
Account a = AccountsRepository.GetAccounts()
.WithID(id)
.FilterTransactions(year, month, day)
.SingleOrDefault();
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。