我有一个问题重用包括EF 5.我做错了什么?

时间:2014-03-17 15:01:58

标签: c# linq entity-framework entity-framework-5

我遇到以下问题的问题。

public IEnumerable<BillingConfiguration> GetBillingConfigurationsForAccount(int accountId, DateTime? effectiveDate)
    {
        var result = new List<BillingConfiguration>();
        var accountIds = new List<int>();
        var billingGroupIds = new List<int>();

        using (var context = new GbContext())
        {
            var account = context.Accounts.Where(a => a.AccountId == accountId).SingleOrDefault();
            if (account.ParentAccountId == null) // This is a parent account
            {
                // Get list of sub account ids
                accountIds = context.Accounts.Where(a => a.ParentAccountId == accountId).Select(x => x.AccountId).ToList();
                accountIds.Add(accountId);
            }
            else // This is a sub account
            {
                accountIds.Add(accountId);
            }

            // Get list of Billing Configurations for all accountIds
            IQueryable<BillingConfigurationAccount> accountQuery = context.BillingConfigurationAccounts
                .Where(a => accountIds.Contains(a.AccountId)).AsQueryable();
            AddBillingConfigurationAccountIncludes(accountQuery);
            var accountBillingConfigurations = accountQuery.ToList();

            //IQueryable<BillingConfigurationAccount> accountQuery = context.BillingConfigurationAccounts;
            //AddBillingConfigurationAccountIncludes(accountQuery);
            //accountQuery = accountQuery.Where(a => accountIds.Contains(a.AccountId));
            //var accountBillingConfigurations = accountQuery.ToList();   

            if (accountBillingConfigurations.HasValues())
            {
                accountBillingConfigurations.ForEach(x => result.Add(x.BillingConfiguration));
            }

            return result;
        }
    }
private void AddBillingConfigurationAccountIncludes(IQueryable<BillingConfigurationAccount> query)
    {
        query = query
            .Include(p => p.BillingConfiguration)
            .Include(p => p.BillingConfiguration.BillingConfigurationLevel)
            .Include(p => p.BillingConfiguration.BillType)
            .Include(p => p.BillingConfiguration.PayerOptionType)
            .Include(p => p.BillingConfiguration.BillModeOptions)
            .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.BillMode))
            .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.FirstPastDueDateOptionsType))
            .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.LapseNoticeOptionsType))
            .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.PaymentDueDateOptionsType))
            .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.PaymentGracePeriodType))
            .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.SecondPastDueDateOptionsType))
            .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.BillModeOptionPaymentMethods))
            .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.BillModeOptionPaymentMethods.Select(n => n.PaymentMethodType)));
    }

当我查看accountBillingConfigurations列表中有2个项目时,BillingConfiguration实体为空,BillingConfigurationId存在并且正确,但它没有带回{ {1}}实体。

现在,如果我像这样内联所有内容,它会按预期工作。 where子句和包含是相同的。

BillingConfiguration

该代码实际上填写了public IEnumerable<BillingConfiguration> GetBillingConfigurationsForAccount(int accountId, DateTime? effectiveDate) { var result = new List<BillingConfiguration>(); var accountIds = new List<int>(); var billingGroupIds = new List<int>(); using (var context = new GbContext()) { var account = context.Accounts.Where(a => a.AccountId == accountId).SingleOrDefault(); if (account.ParentAccountId == null) // This is a parent account { // Get list of sub account ids accountIds = context.Accounts.Where(a => a.ParentAccountId == accountId).Select(x => x.AccountId).ToList(); accountIds.Add(accountId); } else // This is a sub account { accountIds.Add(accountId); } // Get list of Billing Configurations for all accountIds var accountBillingConfigurations = context.BillingConfigurationAccounts .Include(p => p.BillingConfiguration) .Include(p => p.BillingConfiguration.BillingConfigurationLevel) .Include(p => p.BillingConfiguration.BillType) .Include(p => p.BillingConfiguration.PayerOptionType) .Include(p => p.BillingConfiguration.BillModeOptions) .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.BillMode)) .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.FirstPastDueDateOptionsType)) .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.LapseNoticeOptionsType)) .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.PaymentDueDateOptionsType)) .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.PaymentGracePeriodType)) .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.SecondPastDueDateOptionsType)) .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.BillModeOptionPaymentMethods)) .Include(p => p.BillingConfiguration.BillModeOptions.Select(m => m.BillModeOptionPaymentMethods.Select(n => n.PaymentMethodType))) .Where(a => accountIds.Contains(a.AccountId)); if (accountBillingConfigurations.HasValues()) { accountBillingConfigurations.ForEach(x => result.Add(x.BillingConfiguration)); } return result; } } 实体及其所有子实体。 我试图做的是什么?如果,那么我做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

这是一个微妙的。

你正确地假设像

这样的方法
void SomeMethod(IQueryable<T> query)

可以修改它接收的引用对象,并且这些修改将在方法范围之外可见。

然而......该方法没有修改对象,替换它。作业=Include方法的返回值分配给query变量。现在方法中,前一个query变量超出了范围,因为引用被新对象的引用覆盖。但之外的方法这个新的引用永远不会被知道。那里旧的参考仍然适用。

补救措施很简单:返回新参考:

IQueryable<T> SomeMethod(IQueryable<T> query)

在你的情况下,这将成为:

accountQuery = AddBillingConfigurationAccountIncludes(accountQuery);

如果你不相信我,可以在Linqpad尝试这个小程序:)

void Main()
{
    var ints = Enumerable.Range(1,10).ToArray();
    AddInt(ints, 11);
    ints.Dump();
}

void AddInt(IEnumerable<int> list, int i)
{
    list = list.Concat(new[] {i});
    list.Dump();
}