Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 如何在GroupBy中使用Include_C#_Linq_Entity Framework - Fatal编程技术网

C# 如何在GroupBy中使用Include

C# 如何在GroupBy中使用Include,c#,linq,entity-framework,C#,Linq,Entity Framework,这是我的模型的一部分: public class Transaction { public int Id { get; set; } public Decimal Amount { get; set; } public DateTime Date { get; set; } public string Comment { get; set; } public Subcategory TransactionSubcategory { get; set; }

这是我的模型的一部分:

public class Transaction
{
    public int Id { get; set; }
    public Decimal Amount { get; set; }
    public DateTime Date { get; set; }
    public string Comment { get; set; }
    public Subcategory TransactionSubcategory { get; set; }
    public Member OwnerMember { get; set; }
    public int TransactionSubcategoryId { get; set; }
    public int OwnerMemberId { get; set; }
}

public class Subcategory
{
    public Subcategory()
    {
        IsGlobal = false;
        Transactions = new List<Transaction>();
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public Category OwnerCategory { get; set; }
    public List<Transaction> Transactions { get; set; }
    public bool IsGlobal { get; set; }
}

public class Category
{
    public Category()
    {
        IsGlobal = false;
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Subcategory> Subcategories { get; set; }
    public TransactionType TypeOfTransaction { get; set; }
    public Budget OwnerBudget { get; set; }
    public bool IsGlobal { get; set; }
}

public class Member
{
    public Member()
    {
        Transactions = new List<Transaction>();
    }
    public Budget OwnerBudget { get; set; }
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime CreatedTime { get; set; }
    public List<Transaction> Transactions { get; set; }
}
但它仍然是空的。如果我换成

        var members = transactions
            .GroupBy(t => t.OwnerMember, t => t)
            .Include(t=> t.Select(t2 => t2.TransactionSubcategory.OwnerCategory))
            .ToList();
我犯了一个错误:

EntityFramework.dll中发生“System.ArgumentException”类型的异常,但未在用户代码中处理

其他信息:包含路径表达式必须引用在类型上定义的导航属性。使用虚线路径作为参考导航属性,选择运算符作为集合导航属性


第一:从数据库中选择时,应包括以下导航:

from o in context.Transactions.Include("TransactionSubcategory").Include("TransactionSubcategory.OwnerCategory")
...
select o
第二:从这里删除Include

var members = transactions
            .GroupBy(t => t.OwnerMember, t => t)
            .ToList();
第三:我相信您希望按ID分组,而不是按类分组,因为按引用类型分组不会得到预期的结果。具有相同字段的所有类将生成不同的组,因为它们具有不同的引用

var members = transactions
                .GroupBy(t => t.OwnerMember.Id, t => t)
                .ToList();

我找到了解决办法。如果我先做ToList,然后再做GroupBy

var members = transactions
            .Include(t => t.TransactionSubcategory.OwnerCategory)
            .Include(t=> t.OwnerMember).ToList()
            .GroupBy(t => t.OwnerMember, t => t)

.IncludeTransactionSubcategory.IncludeTransactionSubcategory.OwnerCategory有效吗?您是否将该字段定义为外键?1不,它无效2我没有向模型添加任何属性,但我找到了解决方案
var members = transactions
            .Include(t => t.TransactionSubcategory.OwnerCategory)
            .Include(t=> t.OwnerMember).ToList()
            .GroupBy(t => t.OwnerMember, t => t)