Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF:构建IIIncludeableQueryable动态_C#_Asp.net_Entity Framework_Entity Framework Core - Fatal编程技术网

C# EF:构建IIIncludeableQueryable动态

C# EF:构建IIIncludeableQueryable动态,c#,asp.net,entity-framework,entity-framework-core,C#,Asp.net,Entity Framework,Entity Framework Core,我得到了{id},其中包含关系的选项 我想这样做: var account = _context.Accounts.Where(a => a.AccountId == id); if (withActiveSubscription) { account = account.Include(a => a.ActiveSubscription); if (withCharges)

我得到了{id},其中包含关系的选项

我想这样做:

        var account = _context.Accounts.Where(a => a.AccountId == id);
        if (withActiveSubscription)
        {
            account = account.Include(a => a.ActiveSubscription);
            if (withCharges)
            {
                account = account.ThenInclude(s => s.Charges);
            }
        }
但我得到“无法解析符号'ThenClude'”。 我可以把它改成

            if (withCharges)
            {
                account = account.Include(a => a.ActiveSubscription).ThenInclude(s => s.Charges);
            }
但这看起来很糟糕,因为我复制了
帐户。Include(a=>a.ActiveSubscription)
。 那么,我应该如何编写代码呢? (我正在使用EF core)
谢谢

,Include
作为扩展方法提供给
返回的类型。Include(…)
。这种类型不仅仅是
IQueryable
,通过分配给
account
,您可以去掉额外功能的编译时知识。要保留它,请将其存储在额外的helper变量中,该变量可以具有与
帐户不同的类型:

var account = _context.Accounts.Where(a => a.AccountId == id);
if (withActiveSubscription)
{
    var accountAS = account.Include(a => a.ActiveSubscription);
    account = accountAS;

    if (withCharges)
    {
        var accountASC = accountAS.ThenInclude(s => s.Charges);
        account = accountASC;
    }
}