Lambda Express || EF核心|| ABP ||内部联接

时间:2018-11-09 22:58:40

标签: c# entity-framework asp.net-core aspnetboilerplate

我是EF和lambda表达式的新手,这就是我要在Basic SQL语法中实现的目标

SELECT * 
FROM [TestDb].[dbo].[Boxes] BXS
INNER JOIN [TestDb].[dbo].[BoxInventoryLevels] BIL ON BXS.Id = BIL.BoxID
INNER JOIN  [TestDb].[dbo].[Booklets] BLT  ON  BLT.BoxId = BXS.Id
WHERE BLT.DocumentNo = 'ABC09' AND BLT.Status = 2 AND BXS.Status = 1

这是C#中的方法。注意:我希望结果类型仍然是Box的实体

[Table("Boxes")]
public class Box : FullAuditedEntity<Guid>, IMustHaveTenant
{
    public virtual int TenantId { get; set; }
    public virtual Warehouse Warehouse { get; set; }
    [Required]
    public virtual BoxStatus Status { get; set; }
    [Required]
    public virtual string BoxSerialNumber { get; set; }
    [Required]
    public virtual BranchEnumerations.DocumentPage DocumentPage { get; set; }
    [Required]
    public virtual BranchEnumerations.DocumentType DocumentType { get; set; }
    public virtual ICollection<ProductInBoxes> ProductInBoxes { get; set; }
    public virtual ICollection<Booklet> Booklets { get; set; }
    public virtual ICollection<BoxInventoryLevel> BoxInventoryLevels { get;set; } }

............................................... ......

    public async Task<Box> GetOpenBoxAndUnusedBookletByDocumentNo(string DocumentNo)
    {
        var box = _boxRepository
            .GetAllIncluding(bk => bk.Booklets.Where(doc => doc.DocumentNo.Equals(DocumentNo) && doc.Status.Equals(BookletStatus.ReadyToBeUsed))
                             .FirstOrDefault(),
                             dx => dx.BoxInventoryLevels
            .Select(bc => bc.Box).Where(pb => pb.Status.Equals(BoxStatus.Open)))
            .FirstOrDefault();
        if (@box == null)
        {
            throw new UserFriendlyException("Could not find the box details, maybe it's deleted!");
        }
        if (@box.Booklets == null)
        {
            throw new UserFriendlyException("Could not find the booklet details, maybe it's deleted!");
        }
        await Task.Delay(1);
        return box;
    }

执行代码时出现运行时错误

  

错误2018-11-09 23:45:14,091 [6]   Mvc.ExceptionHandling.AbpExceptionFilter-包含属性lambda   表达式'bk => {来自bk.Booklets中的Booklet文档,其中   ([doc] .DocumentNo.Equals(“ ABC09”)And [[doc] .Status.Equals(2))   选择[doc] => FirstOrDefault()}'无效。表达式应   代表属性访问:“ t => t.MyProperty”。达到目标   在派生类型上声明的导航,指定显式类型   目标类型的lambda参数,例如'(衍生d)=>   d.MyProperty”。有关包括相关数据的更多信息,请参见   http://go.microsoft.com/fwlink/?LinkID=746393。   System.InvalidOperationException:包含属性lambda   表达式'bk => {来自bk.Booklets中的Booklet文档,其中   ([doc] .DocumentNo.Equals(“ A09581266”)And [[doc] .Status.Equals(2))   选择[doc] => FirstOrDefault()}'无效。表达式应   代表属性访问:“ t => t.MyProperty”。达到目标   在派生类型上声明的导航,指定显式类型   目标类型的lambda参数,例如'(衍生d)=>   d.MyProperty”。

0 个答案:

没有答案