代码之家  ›  专栏  ›  技术社区  ›  David

Linq2Sql内部列表对象上的管道和过滤器

  •  0
  • David  · 技术社区  · 16 年前

    我正在使用带有管道/过滤器的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。

    2 回复  |  直到 16 年前
        1
  •  0
  •   Spence    16 年前

        2
  •  0
  •   David    16 年前