【问题标题】:How do I outer join multiple tables using linq to entities如何使用 linq 将多个表外部连接到实体
【发布时间】:2011-02-11 04:09:22
【问题描述】:

我将 ASP.NET MVC 3 与 Entity Framework CodeFirst 一起使用

我有两个类如下:

问题:

public class Question
{
    public int ID { get; set; }
    public Target Target { get; set; }
    public string QuestionText { get; set; }
    public Category Category { get; set; }
    public QuestionType QuestionType { get; set; }
    public virtual ICollection<Answer> Answers { get; set; }
    public Unit Unit { get; set; }
}

答案:

public class Answer
{
    public int ID { get; set; }
    public virtual Question Question { get; set; }
    public string AnswerText { get; set; }
    public int Value { get; set; }
}

我也有这个 ViewModel:

public class QuestionViewModel
{
    public int ID { get; set; }
    public string Question { get; set; }
    public string QuestionType { get; set; }
    public string Target { get; set; }
    public string Category { get; set; }
    public string Unit { get; set; }
    public List<Answer> Answers { get; set; }
}

我想查询问题表并包含答案(如果有的话)。

我一直在尝试这种风格

        var question = (from q in hontgen.Questions
                        where q.ID == id
                        join qt in db.QuestionTypes on q.QuestionType equals qt
                        join t in db.Targets on q.Target equals t
                        join c in db.Categories on q.Category equals c
                        join u in db.Units on q.Unit equals u
                        join a in db.Answers on q.Answers equals a

                        select new QuestionViewModel() {
                            ID = q.ID,
                            Question = q.QuestionText,
                            QuestionType = qt.Type,
                            Category = c.CategoryName,
                            Unit = u.UnitName,
                            Target = t.TargetName,
                            Answers = a
                        }).Single();

但这当然不会滚动,因为 a 不是答案列表,而只是一个答案。

如何重写查询以获取集合中的所有答案,或“问题”中具有正确问题的所有答案,同时接受一个空的答案列表?

【问题讨论】:

    标签: entity-framework-4 linq-to-entities code-first


    【解决方案1】:

    像下面这样的子查询呢

    public class DataRepository
    {
        public List<Question> Questions { get; set; }
        public IEnumerable<Answer> Answers { get; set; }
    
    }
    
    
    public class QandA
    {
    
        DataRepository dr = new DataRepository();
    
        public void QueryQuestion(int id)
        {
            var question = (from q in dr.Questions
                            where q.ID == id
    
                            select new QuestionViewModel()
                            {
                                ID = q.ID,
                                Question = q.QuestionText,
                                Answers = (from a in dr.Answers 
                                            where a.Question == q
                                            select a)
                            });
        }
    
    }
    

    }

    【讨论】:

    • 您好,感谢您的快速回复,我现在正在尝试(我的代码当时有点乱,但我正在整理它)。它编译得很好,看起来就像它可以完成这项工作一样。但是,如果我没有答案,.ToList() 会发生什么?
    • 我不断收到这个.. 有什么想法吗? LINQ to Entities does not recognize the method 'System.Collections.Generic.List1[Project.Models.Answer] ToListHow to Answer(System.Collections.Generic.IEnumerable1[Project.Models.Answer])' method, and this method cannot be translated into a store expression.
    • 在存储库中如果您将 List 答案放入 IEnumerable 答案中会怎样;然后你不调用 ToList() 函数,查询将运行。然后,如果您以后需要列表的功能,您可以在 Linq to Entities 查询之外使用 Answers.ToList()。
    • 你好,我做了你所说的(或多或少),但我得到了一个例外:Unable to create a constant value of type 'Project.Models.Answer'. Only primitive types ('such as Int32, String, and Guid') are supported in this context. 无法理解这个......这次也有任何线索吗?对象不能枚举吗?
    • 要解决此问题,请尝试在连接上使用实体的属性,而不是整个实体。例如,在 Id 字段而不是实体上加入 Targets。 Example 'join t in db.Targets on q.Target.TargetId equals t.TargetId
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 2012-12-08
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多