我有此寄售实体模型。
public Guid Id { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public bool? Deleted { get; set; }
public string Status { get; set; }
public string Services { get; set; }
public string CustomerReference { get; set; }
public string ConsignmentNote { get; set; }
public int? TotalPieces { get; set; }
...
public virtual ICollection<ConsignmentDocument> ConsignmentDocument { get; set; }
public virtual ICollection<ConsignmentLine> ConsignmentLine { get; set; }
public virtual ICollection<Legging> Legging { get; set; }
现在,我在提取寄售货物时面临的问题是,我没有在响应正文中获取ConsignmentLine,Legging,ConsignmentDocument的任何数据。我先在寄售控制器中仅使用ConsignmentLine对其进行了测试。这是我的GetConsignment()控制器方法。
// GET: api/Consignments
[HttpGet("{id}")]
public async Task<ActionResult<Consignment>> GetConsignment(Guid id)
{
var consignment = await _context.Consignment
.Include(conline => conline.ConsignmentLine.Where(cl => cl.ConsignmentId == cl.Consignment.Id))
.ToListAsync();
return consignment;
}
我的ConsignmentLine.cs是这个:
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal? Length { get; set; }
public decimal? Width { get; set; }
public decimal? Height { get; set; }
public decimal? Volume { get; set; }
public int? Pieces { get; set; }
public decimal? Weigth { get; set; }
public bool? DangerousGoods { get; set; }
public string DgClass { get; set; }
public string UnNumber { get; set; }
public Guid? ConsignmentId { get; set; }
public Guid? ItemId { get; set; }
public Guid? CommodityId { get; set; }
public bool? Deleted { get; set; }
我的Legging.cs是这个:
public Guid Id { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public decimal? Cost { get; set; }
public string LegType { get; set; }
public string FromLeg { get; set; }
public string ToLeg { get; set; }
public Guid? CarrierAccount { get; set; }
public Guid? ConsignmentId { get; set; }
public bool? Deleted { get; set; }
我正在从邮递员进行测试,它返回的Lamba表达式无效。那应该是什么表情?
正如评论所说,您不能在“包含”中使用任何过滤器。
并且GetConsignment方法返回寄售类型,而不是列表集,因此,您需要通过FirstOrDefaultAsync
或SingleOrDefaultAsync
转换寄售变量。
尝试将代码更改为以下内容:
public async Task<ActionResult<Consignment>> GetConsignment(Guid id)
{
var consignment = await _context.Consignment.Where(x=>x.Id == id)
.Include(conline => conline.ConsignmentLine)
.FirstOrDefaultAsync();
return consignment;
}
这是我的ConsignmentLine.cs:
public class ConsignmentLine
{
public int Id { get; set; }
}
要在linq中加载相关数据,可以参考this。
问题出在Lambda表达式上。截至目前,您无法过滤Include语句。如果要过滤结果,则必须将它们全部提取到内存中并进行过滤。就您而言,您甚至不需要过滤包含部分。仅包含导航属性就可以正常工作。假设使用该参数过滤寄售表。
// GET: api/Consignments
[HttpGet("{id}")]
public async Task<ActionResult<Consignment>> GetConsignment(Guid id)
{
var consignment = await _context.Consignment.Where(x=>x.Id == id)
.Include(conline => conline.ConsignmentLine)
.FirstOrDefault();
return consignment;
}
上面的代码将简单地为您提供Consignmet
表及其ConsignmentLine
中的记录。尝试让我知道是否可行。
快乐编码。<3