C# 如何将左侧外部联接查询的匿名类型结果转换为强类型

C# 如何将左侧外部联接查询的匿名类型结果转换为强类型,c#,linq,entity-framework-6,type-conversion,anonymous-types,C#,Linq,Entity Framework 6,Type Conversion,Anonymous Types,下面是对带答案的左外连接问题的查询 var questions = db.Questions .Include(f => f.Category) .Where(p => p.Category_Id == forum.Category_Id) .GroupJoin( db.Answers.Where(p => p.Forum_Id == forum.Id), q => q.Id, a =>

下面是对带答案的左外连接问题的查询

    var questions = db.Questions
    .Include(f => f.Category)
    .Where(p => p.Category_Id == forum.Category_Id)
    .GroupJoin(
        db.Answers.Where(p => p.Forum_Id == forum.Id),
        q => q.Id,
        a => a.Question_Id,
        (q, a) => new { Question = q, Answers = a.FirstOrDefault() }
   ).ToList();
我正在寻找作为IEnumerable的结果,在那里我可以访问问题对象的导航属性,例如

foreach (var question in questions)
{
    Console.WriteLine("Question: {0}", question.Text);
    if (question.Answers != null)
    {
        foreach (var answer in question.Answers)
        {
            Console.WriteLine("Answer: {0}", answer.Text);    
        }                    
    }        
}
此外,以下是我如何建立实体之间的关系:(请建议任何改进)

modelBuilder.Entity()
.HasRequired(f=>f.Category)
.WithMany(f=>f.Forums)
.HasForeignKey(f=>f.Category_Id);
modelBuilder.Entity()
.HasRequired(f=>f.Category)
.有很多(q=>q.问题)
.HasForeignKey(f=>f.Category\u Id)
.WillCascadeOnDelete(假);
modelBuilder.Entity()
.HasRequired(q=>q.Forum)
.有许多(k=>k个答案)
.HasForeignKey(f=>f.Forum_Id);
modelBuilder.Entity()
.HasRequired(f=>f.Question)
.有许多(f=>f.答案)
.HasForeignKey(f=>f.Question\u Id)
.WillCascadeOnDelete(假);
公开课论坛
{
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共Guid类别_Id{get;set;}
公共虚拟类别{get;set;}
公共虚拟ICollection答案{get;set;}
公众论坛()
{
this.Answers=newhashset();
}
}
公共类类别
{
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection论坛{get;set;}
公共虚拟ICollection问题{get;set;}
公共类别()
{
this.Questions=newhashset();
this.Forums=newhashset();
}
}
公开课问题
{
公共Guid Id{get;set;}
公共字符串文本{get;set;}
公共虚拟ICollection答案{get;set;}
公共Guid类别_Id{get;set;}
公共虚拟类别{get;set;}
公众问题(
{
this.Answers=newhashset();
}
}
公开课答案
{
公共Guid Id{get;set;}
公共字符串文本{get;set;}
公共Guid问题_Id{get;set;}
公共虚拟问题{get;set;}
公共Guid论坛_Id{get;set;}
公共虚拟论坛{get;set;}
公众答覆(
{
}
}

为什么问题有
和许多(q=>q.questions)
而答案有
和许多(k=>k.Answers)
?我发现在你的类中没有任何地方?在modelbuilder中,问题类的导航属性类别指向多个(q=>q.Questions),答案也是如此。为什么问题有
多个(q=>q.Questions)
和答案有
多个(k=>k.Answers)
?在modelbuilder中,问题类的导航属性类别指向许多(q=>q.Questions),答案也是如此。
    modelBuilder.Entity<Forum>()
        .HasRequired<Category>(f => f.Category)
        .WithMany(f => f.Forums)
        .HasForeignKey(f => f.Category_Id);

    modelBuilder.Entity<Question>()
        .HasRequired<Category>(f => f.Category)
        .WithMany(q => q.Questions)
        .HasForeignKey(f => f.Category_Id)
        .WillCascadeOnDelete(false);

    modelBuilder.Entity<Answer>()
        .HasRequired<Forum>(q => q.Forum)
        .WithMany(k => k.Answers)
        .HasForeignKey(f => f.Forum_Id);

    modelBuilder.Entity<Answer>()
        .HasRequired<Question>(f => f.Question)
        .WithMany(f => f.Answers)
        .HasForeignKey(f => f.Question_Id)
        .WillCascadeOnDelete(false);



public class Forum
{
    public Guid Id { get; set; }

    public string Name { get; set; }
    public Guid Category_Id { get; set; }
    public virtual Category Category { get; set; }
    public virtual ICollection<Answer> Answers { get; set; }
    public Forum()
    {
        this.Answers = new HashSet<Answer>();
    }
}

public class Category
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Forum> Forums { get; set; }
    public virtual ICollection<Question> Questions { get; set; }

    public Category()
    {
        this.Questions = new HashSet<Question>();
        this.Forums = new HashSet<Forum>();
    }
}

public class Question
{
    public Guid Id { get; set; }
    public string Text { get; set; }
    public virtual ICollection<Answer> Answers { get; set; }
    public Guid Category_Id { get; set; }
    public virtual Category Category { get; set; }
    public Question()
    {
        this.Answers = new HashSet<Answer>();
    }
}

public class Answer
{
    public Guid Id { get; set; }
    public string Text { get; set; }
    public Guid Question_Id { get; set; }
    public virtual Question Question { get; set; }
    public Guid Forum_Id { get; set; }
    public virtual Forum  Forum { get; set; }
    public Answer()
    {

    }
}