C# LINQ查询:获取所有字段,包括外键

C# LINQ查询:获取所有字段,包括外键,c#,.net,linq,entity-framework,C#,.net,Linq,Entity Framework,我有以下查询以按包含日期获取结果: var q = from x in db.TableX where x.Timestamp.CompareTo(fromDate) >= 0 && x.Timestamp.CompareTo(toDate) <= 0 select x; 因为您使用的是延迟加载,所以需要将导航属性定义为虚拟属性,并且需要启用

我有以下查询以按包含日期获取结果:

var q = from x in db.TableX
                    where x.Timestamp.CompareTo(fromDate) >= 0
                       && x.Timestamp.CompareTo(toDate) <= 0
                    select x;

因为您使用的是延迟加载,所以需要将导航属性定义为虚拟属性,并且需要启用代理创建,以便EF可以围绕您的类创建代理,并在需要时覆盖这些属性以进行加载

public class TableX
    {        
        public  int Id { get; set; }
        public string str1{ get; set; } 
        public virtual Table2 t1{ get; set; }
        public virtual Table3 t2{ get; set; }
        public virtual  Table4 t3{ get; set; }
        public virtual Table5 t4{ get; set; }
        public virtual Tablet5 t5{ get; set; } 
     }

你能发布你的TableX类吗?延迟加载是默认的吗?我如何更改它?
db.Configuration.LazyLoadingEnabled=true啊,太棒了!这里有更多信息。
public class TableX
    {        
        public  int Id { get; set; }
        public string str1{ get; set; } 
        public virtual Table2 t1{ get; set; }
        public virtual Table3 t2{ get; set; }
        public virtual  Table4 t3{ get; set; }
        public virtual Table5 t4{ get; set; }
        public virtual Tablet5 t5{ get; set; } 
     }