简体   繁体   English

在EF6 C#中订购急切加载的导航属性数据

[英]Order Eager Loaded Navigation Property Data In EF6 C#

I am Struggling to find a suitable answer that applies to to the scenario I am struggling with on StackOverflow. 我正在努力找到适用于我在StackOverflow上遇到困难的情况的合适答案。 I am attempting to order Eager Loaded navigation property data, but I'm not sure about how to write the Linq query correctly. 我正在尝试订购“渴望加载”的导航属性数据,但不确定如何正确编写Linq查询。

My Types are as follows, 我的类型如下

This is the parent type: 这是父类型:

    public class Diary
{
    public Diary()
    {
        Appointments = new List<Appointment>();
    }
    public int Id{ get; set; }
    public DateTime DiaryDate { get; set; }
    public List<Appointment> Appointments { get; set; }

}

} }

This is the child type: 这是子类型:

public class Appointment
{
    public int Id { get; set; }
    public Office Office { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
    public string Comment { get; set; }

}

The Office enum is, Office枚举是

public enum
{
Office1,
Office2,
Office3,
Office4
}

And my repository code that I attempted that would return an individual Diary with its linked appointments is as follows. 我尝试的存储库代码将返回带有链接约会的单个日记,如下所示。 I am not wanting to filter all of the data, I just want to be able to order the navigation property by the office num so my eventual output will look something similar to this on screen 我不想过滤所有数据,我只想能够按办公室编号排序导航属性,以便最终的输出在屏幕上看起来类似于此

Diary - DiaryDate 日记-DiaryDate

Office 1 <--office 1 appointments here--> 办公室1 <-办公室1在这里约会->

Office 2 办公室2

<--office 2 appointments here--> <-这里有2个办公室约会->

etc. 等等

Repository code as follows 仓库代码如下

        public Diary ReturnDiaryById(int id)
    {
        using(DiaryContext context = new DiaryContext())

      {
        var returnDiary = context.Diary.AsNoTracking().Include(a => a.Issues)
        .Select(b => new Diary
        {
            Appointments = (List<Appointment>)b.Appointments.OrderBy(c => c.Office),
            Id = b.Id,
            DiaryDate = b.DiaryDate
        }

            ).FirstOrDefault();


        return returnDiary;

        }
     }

This code compiles but at runtime it compiles of a not supported exception. 该代码可以编译,但是在运行时它将编译不支持的异常。

I hope I have explained what I am trying to achieve and that my code roughly infers it, if not I can clarify anything, 我希望我已经解释了我要实现的目标,并且我的代码可以大致推断出这一点,如果没有,我可以澄清任何事情,

thank you to any offers of help in advice, all input is greatly appreciated 感谢您提供任何建议方面的帮助,我们将不胜感激所有输入

Probably EF is struggling to understand the translation of your ENUM orderby to a SQL query. EF可能很难理解您的ENUM订单通过SQL查询的转换。

In your case, there is not much performance difference between doing the ordering on the SQL side vs. doing it on the C# side.. (since we already bring in the single diary with all appointments) 在您的情况下,在SQL端进行排序与​​在C#端进行排序之间没有太大的性能差异。(因为我们已经携带了所有约会的单个日记)

So try this query and let us know if this works: 因此,请尝试执行以下查询,让我们知道是否可行:

public Diary ReturnDiaryById(int id)
{
 using(var context = new DiaryContext())
 {
  var returnDiary = context.Diary.AsNoTracking().Include(a => a.Appointments)
                           .FirstOrDefault(d => d.Id == id);

  if (returnDiary != null)
  {
   returnDiary.Appointments = returnDiary.Appointments.OrderBy(c => c.Office.ToString()).ToList();
  } 

  return returnDiary ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM