尝试从Entity Framework Core中的实体加载单个子属性时发生异常

问题描述 投票:0回答:1

我试图在我的“实体”实体中获取一个“数据”。我可以从这句话中得到全部:

。ThenInclude(matter => issue.Data)

但是我只想要1 =>,它具有创建的最新日期。如下所示,但它不起作用。

。ThenInclude(matter => issue.Data.OrderByDescending(i => i.CreatedDate).FirstOrDefault())

我的.ThenInclude()异常。它说表达式应该代表一个属性。 .Data不是财产吗?我也尝试了没有.Data的情况,没有运气

我的例外说:

“ ThenInclude属性lambda表达式'matter => {来自MatterData i in物质。数据按[i] .CreatedDate desc选择[i] => FirstOrDefault()}。数据”无效。该表达式应表示属性访问:“ t => t.MyProperty”。要定位在派生类型上声明的导航,请指定目标类型的显式输入的lambda参数,例如。 '(派生d)=> d.MyProperty'。“

这是我试图获取'Data'的单个属性的方式>

public async Task<Claim> GetClaim(int id)
{
   var query = _autoContext.Claims
     .Include(claim => claim.Matter)
         .ThenInclude(matter => matter.Data.OrderByDescending(i => i.CreatedDate).FirstOrDefault().Data)
     .AsQueryable();

   var result = await query.FirstOrDefaultAsync(c => c.Id == id);
   return result;
}

这里是理赔,事项和事项数据

public class Matter
{
    public Matter()
    {
        CreatedDate = DateTime.Now;
        EditedDate = DateTime.Now;
        Data = new List<MatterData>();
        Claims = new List<Claim>();
    }
    public int Id { get; set; }
    public string MatterNumber { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set;}
    public ICollection<MatterData> Data { get; set; }
    public ICollection<Claim> Claims { get; set; }
    public DateTime CreatedDate { get; set; }
    public User CreatedBy { get; set; }
    public DateTime EditedDate { get; set; }
    public User EditedBy { get; set; }
}

public class MatterData
{
    public MatterData()
    {
        CreatedDate = DateTime.Now;
    }
    public int Id { get; set; }
    public string Data { get; set; } 
    public Matter Matter { get; set; }
    public DateTime CreatedDate { get; set; }
    public User CreatedBy { get; set; }
}

public class Claim
{
    public Claim()
    {
        CreatedDate = DateTime.Now;
        EditedDate = DateTime.Now;
        InjuredParty = new List<Injured>();
    }
    public int Id { get; set; }
    public int? ClaimNumber { get; set; }
    public Matter Matter { get; set; }
    public TrustGroup TrustGroup { get; set; } 
    public Attorney Attorney { get; set; }
    public User Contact { get; set; }
    public Status ClaimStatus { get; set; }
    public ClaimType ClaimType { get; set; }
    public bool Approved { get; set; }
    public DateTime? DateApproved { get; set; }
    public DateTime? DateExported { get; set; }
    public ICollection<Injured> InjuredParty { get; set; }
    public DateTime CreatedDate { get; set; }
    public User CreatedBy { get; set; }
    public DateTime EditedDate { get; set; }
    public User EditedBy { get; set; }

}

我试图在我的“实体”实体中获取一个“数据”。我可以从以下语句中获取全部信息:.ThenInclude(matter => issue.Data),但我只想在其中创建了最新日期的= 1 =>。 ...

c# linq-to-sql entity-framework-core linq-to-entities
1个回答
0
投票

此行

© www.soinside.com 2019 - 2024. All rights reserved.