EF 5 Linq相关实体始终为空
我有一个实体AllocatedContainers,它具有InventoryContainerDetails的外键 当我执行下面的linq查询时,它会返回在AllocatedContainers中没有相关表项的记录,但如果AllocatedContainer中应该有一个连接的记录,则不会返回整行。不知道为什么 下面是类,请注意与AllocatedContainer的虚拟关系EF 5 Linq相关实体始终为空,linq,c#-4.0,entity-framework-5,Linq,C# 4.0,Entity Framework 5,我有一个实体AllocatedContainers,它具有InventoryContainerDetails的外键 当我执行下面的linq查询时,它会返回在AllocatedContainers中没有相关表项的记录,但如果AllocatedContainer中应该有一个连接的记录,则不会返回整行。不知道为什么 下面是类,请注意与AllocatedContainer的虚拟关系 public partial class InventoryContainerDetail { public In
public partial class InventoryContainerDetail
{
public InventoryContainerDetail()
{
this.AllocatedContainers = new HashSet<AllocatedContainer>();
}
public int Id { get; set; }
public int InventoryContainerHeaderId { get; set; }
public int ItemId { get; set; }
public Nullable<int> ReceiptDetailId { get; set; }
public decimal QtyInContainer { get; set; }
public virtual InventoryContainerHeader InventoryContainerHeader { get; set; }
public virtual Item Item { get; set; }
public virtual ICollection<AllocatedContainer> AllocatedContainers { get; set; }
}
下面是存储库方法。注意:我正在通过引导热切加载。包括希望它工作,但没有运气:
public override IQueryable<InventoryContainerDetail> SearchFor(Expression<Func<InventoryContainerDetail, bool>> predicate, Expression<Func<InventoryContainerDetail, bool>> orderbylinq = null)
{
if (orderbylinq == null)
{
return DbSet.Include("AllocatedContainers").Where(predicate);
}
else
{
return DbSet.Include("AllocatedContainers").Where(predicate).OrderBy(orderbylinq);
}
}
public override IQueryable SearchFor(表达式谓词,表达式orderbylinq=null)
{
if(orderbylinq==null)
{
返回DbSet.Include(“AllocatedContainers”)。其中(谓词);
}
其他的
{
返回DbSet.Include(“AllocatedContainers”).Where(谓词).OrderBy(orderbylinq);
}
}
好的,实际上它正在工作……我改变了
“&&(x.QtyInContainer-x.AllocatedContainers.Sum(a=>a.allocatedcontainer))>0”
对此
”&&&(x.QtyInContainer-(x.AllocatedContainers.Count()==0?0:x.AllocatedContainers.Sum(a=>a.allocatedcontainer)))>0“
感谢Ladislav向我展示了这个技巧…然后你可以使用实际的sql并将其粘贴到sql Server中,并从那里帮助调试,哪一个很棒DbSet的代码是什么?请在查询中使用
ToString
而不是ToList
,以获取生成的SQL并将其添加到您的问题中?感谢@LasislavMrnka查看。我已经添加了生成的SQL。好的,实际上它正在工作…我将“&&&(x.QtyInContainer-x.AllocatedContainers.Sum(a=>a.AllocatedQty))>0”更改为“&&(x.QtyInContainer-(x.AllocatedContainers.Count()==0?0:x.AllocatedContainers.Sum(a=>a.AllocatedQty))>0”。感谢Ladislav向我展示了这个技巧…然后,您可以将实际的sql粘贴到sql Server中,并从那里帮助调试,这很好。在调试模式下单步执行代码时,您可以将鼠标悬停在将成为查询结果的任何变量上,并让调试器向您显示sql(使用相同的.ToString()方法)在调用.ToList()的部分之前,您可以将其从调试窗口复制到SQL MS
SELECT
[Project2].[Id1] AS [Id],
[Project2].[Id] AS [Id1],
[Project2].[InventoryContainerHeaderId] AS [InventoryContainerHeaderId],
[Project2].[ItemId] AS [ItemId],
[Project2].[ReceiptDetailId] AS [ReceiptDetailId],
[Project2].[QtyInContainer] AS [QtyInContainer],
[Project2].[CreatedById] AS [CreatedById],
[Project2].[CreatedOn] AS [CreatedOn],
[Project2].[ModifiedById] AS [ModifiedById],
[Project2].[ModifiedOn] AS [ModifiedOn],
[Project2].[C1] AS [C1],
[Project2].[Id2] AS [Id2],
[Project2].[AllocationNeedId] AS [AllocationNeedId],
[Project2].[AllocatedContainerDetailId] AS [AllocatedContainerDetailId],
[Project2].[AllocatedQty] AS [AllocatedQty],
[Project2].[CreatedById1] AS [CreatedById1],
[Project2].[CreatedOn1] AS [CreatedOn1],
[Project2].[ModifiedById1] AS [ModifiedById1],
[Project2].[ModifiedOn1] AS [ModifiedOn1]
FROM ( SELECT
[Project1].[Id] AS [Id],
[Project1].[InventoryContainerHeaderId] AS [InventoryContainerHeaderId],
[Project1].[ItemId] AS [ItemId],
[Project1].[ReceiptDetailId] AS [ReceiptDetailId],
[Project1].[QtyInContainer] AS [QtyInContainer],
[Project1].[CreatedById] AS [CreatedById],
[Project1].[CreatedOn] AS [CreatedOn],
[Project1].[ModifiedById] AS [ModifiedById],
[Project1].[ModifiedOn] AS [ModifiedOn],
[Project1].[Id1] AS [Id1],
[Extent4].[Id] AS [Id2],
[Extent4].[AllocationNeedId] AS [AllocationNeedId],
[Extent4].[AllocatedContainerDetailId] AS [AllocatedContainerDetailId],
[Extent4].[AllocatedQty] AS [AllocatedQty],
[Extent4].[CreatedById] AS [CreatedById1],
[Extent4].[CreatedOn] AS [CreatedOn1],
[Extent4].[ModifiedById] AS [ModifiedById1],
[Extent4].[ModifiedOn] AS [ModifiedOn1],
CASE WHEN ([Extent4].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM (SELECT
[Extent1].[Id] AS [Id],
[Extent1].[InventoryContainerHeaderId] AS [InventoryContainerHeaderId],
[Extent1].[ItemId] AS [ItemId],
[Extent1].[ReceiptDetailId] AS [ReceiptDetailId],
[Extent1].[QtyInContainer] AS [QtyInContainer],
[Extent1].[CreatedById] AS [CreatedById],
[Extent1].[CreatedOn] AS [CreatedOn],
[Extent1].[ModifiedById] AS [ModifiedById],
[Extent1].[ModifiedOn] AS [ModifiedOn],
[Extent2].[Id] AS [Id1],
[Extent2].[FacilityId] AS [FacilityId],
(SELECT
SUM([Extent3].[AllocatedQty]) AS [A1]
FROM [dbo].[AllocatedContainers] AS [Extent3]
WHERE [Extent1].[Id] = [Extent3].[AllocatedContainerDetailId]) AS [C1]
FROM [dbo].[InventoryContainerDetail] AS [Extent1]
INNER JOIN [dbo].[InventoryContainerHeader] AS [Extent2] ON [Extent1].[InventoryContainerHeaderId] = [Extent2].[Id] ) AS [Project1]
LEFT OUTER JOIN [dbo].[AllocatedContainers] AS [Extent4] ON [Project1].[Id] = [Extent4].[AllocatedContainerDetailId]
WHERE (([Project1].[QtyInContainer] - [Project1].[C1]) > cast(0 as decimal(18))) AND ([Project1].[FacilityId] = 1) AND ([Project1].[ItemId] = 3027)
) AS [Project2]
ORDER BY [Project2].[CreatedOn] ASC, [Project2].[Id1] ASC, [Project2].[Id] ASC, [Project2].[C1] ASC
public override IQueryable<InventoryContainerDetail> SearchFor(Expression<Func<InventoryContainerDetail, bool>> predicate, Expression<Func<InventoryContainerDetail, bool>> orderbylinq = null)
{
if (orderbylinq == null)
{
return DbSet.Include("AllocatedContainers").Where(predicate);
}
else
{
return DbSet.Include("AllocatedContainers").Where(predicate).OrderBy(orderbylinq);
}
}