Entity framework 在ASP dotnet Core API中返回对象内的多个列表数据

Entity framework 在ASP dotnet Core API中返回对象内的多个列表数据,entity-framework,linq,.net-core,controller,asp.net-core-webapi,Entity Framework,Linq,.net Core,Controller,Asp.net Core Webapi,我有这个寄售实体模型 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 Cu

我有这个寄售实体模型

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; }
我的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; }

我从postman测试它,它返回Json异常。那么表达式应该是什么呢?

正如评论所说,在Include中不能使用任何过滤

getAudition方法返回寄售类型,而不是列表集,因此,需要通过
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; 
}

要在linq中加载相关数据,可以参考。

问题在于Lambda表达式。到目前为止,您无法筛选Include语句。如果你想要一个过滤的结果,你必须把它们全部提取到内存中并过滤它们。在您的情况下,您甚至不需要过滤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;
    }

愉快的编码。你能推荐其他方法吗?正如回答中所说:你甚至不需要
Where
。但是FirstOrDefault()返回一个错误!代码中没有
FirstOrDefault
,仅仅告诉它“返回错误”是没有意义的。另外,你真的应该展示完整的课程。看起来您在JSON序列化中遇到了臭名昭著的循环引用错误。很容易找到解决方法。@GertArnold谢谢。解决了。必须安装NewtonsoftJson包并将其注入服务。请您再看一下我的问题。我也提供了我的托运线路和绑腿课程。更新的控制器代码向我抛出了一个-JSON对象异常。那么这可能是JSON序列化程序的问题。我已经更新了答案。请看一下。是的,这是JSON序列化程序的问题。昨天修好了。谢谢你的建议。我已经更新了我的问题。你能再看一下吗?@ShakerKamal,我使用了你的委托行模型,结果与我的演示相同,你可以使用我的代码进行测试,不要使用where in Include。并使用FirstOrDefaultAsync。
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; 
}
 public class ConsignmentLine
{
    public int Id { get; set; }
}
    // 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;
    }
services.AddMvc()
    .AddJsonOptions(
        options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
    );