C# 仍然可以';不要让LINQ Include()工作——尽管其他帖子解释了如何工作
我使用的是EF 5.0.0,当我通过TabNumber进行查询时,我试图急切地加载飞机的PerformancePackages,但无论我将其放在查询中的什么位置,我都无法使其工作 我的存储库中的相关代码:C# 仍然可以';不要让LINQ Include()工作——尽管其他帖子解释了如何工作,c#,linq,entity-framework,entity-framework-5,C#,Linq,Entity Framework,Entity Framework 5,我使用的是EF 5.0.0,当我通过TabNumber进行查询时,我试图急切地加载飞机的PerformancePackages,但无论我将其放在查询中的什么位置,我都无法使其工作 我的存储库中的相关代码: public class ReadOnlyRepository<T> : IGenericRepository<T> where T : class { private bool disposed; protected DbContext con
public class ReadOnlyRepository<T> : IGenericRepository<T>
where T : class
{
private bool disposed;
protected DbContext context;
protected DbQuery<T> dataset;
public ReadOnlyRepository() : this(null) { }
public ReadOnlyRepository(DbContext context)
{
this.context = context ?? new MyDataContext();
dataset = this.context.Set<T>().AsNoTracking();
disposed = false;
if (dataset.IsNull())
throw new ArgumentException(string.Format("Cannot create dataset of type {0} from supplied context.", "context", typeof(T).Name));
}
public IQueryable<T> All()
{
return dataset;
}
}
附加编辑:
public class ReadOnlyRepository<T> : IGenericRepository<T>
where T : class
{
private bool disposed;
protected DbContext context;
protected DbQuery<T> dataset;
public ReadOnlyRepository() : this(null) { }
public ReadOnlyRepository(DbContext context)
{
this.context = context ?? new MyDataContext();
dataset = this.context.Set<T>().AsNoTracking();
disposed = false;
if (dataset.IsNull())
throw new ArgumentException(string.Format("Cannot create dataset of type {0} from supplied context.", "context", typeof(T).Name));
}
public IQueryable<T> All()
{
return dataset;
}
}
我试图通过使用.Take(10)
并取消.GroupBy()
(以及将.Include()
移动到.Where()
之前)来简化查询,但它仍然返回0个结果:
var results = repo.All()
.Include(p => p.PerformancePackages)
.Take(10)
我也尝试过使用.Include(“PerformancePackages”)
的老式方法,但这也不起作用:
var results = repo.All()
.Include("PerformancePackages")
.Take(10);
我期望的输出是每架飞机的“超过零”性能包(因为数据库中没有没有没有性能包的飞机)。我已经模拟了一个计数器,以便很容易地看到我得到了多少性能包,到目前为止,计数始终为零:
List<int> counter = new List<int>(results.Count());
foreach(var ap in airplanes)
{
counter.Add(ap.PerformancePackages.Count());
}
正如这里()所解释的,当您将要投影的数据的形状更改为实体以外的任何对象时,Include()将不起作用
在这种情况下,GroupBy将最终结果更改为一组IGrouping,这将使上述条件无效。在没有分组的情况下进行测试,您应该看到它是有效的 问题是有人在数据库中捣乱,该用户查看该表的权限不知何故“丢失”。我恢复了权限,一切正常。您是否尝试过将
Include
移动到Where
call之前?在询问要包含的问题时,您期望的输出以及实际输出是什么非常重要。showrepo.All()
implementation我在“新的和改进的”帖子中提到了这些评论。。。仍然没有乐趣。连接两个表的键是Model
+TabNumber
,至少TabNumber
似乎是一个字符串,对吗?飞机
中的PK值和性能包
中的FK值是否完全匹配(包括外壳、前导/尾随空格等)?我已经扩展了我的解释;那篇文章是我最先读到的东西之一,我想啊哈!这是我的问题-但我已经删除了groupby和大量其他东西(如上所述),但没有任何效果。
counter[0] = 0;
counter[1] = 0;
counter[2] = 0;
counter[3] = 0;
counter[4] = 0;
counter[5] = 0;
counter[6] = 0;
counter[7] = 0;
counter[8] = 0;
counter[9] = 0;