.net 在实体模型中使用ICollection列表进行筛选

z4bn682m  于 2022-12-14  发布在  .NET
关注(0)|答案(1)|浏览(128)

我有密码。
request.ModelId = 2d3c306f-5904-4ca2-b17f-1ace38f2b53e的示例值

var query = _context.Agencies.Select(x => x);

if (request.ModelId != null && request.ModelId != Guid.Empty)
{
    query = query.Where(a => a.Recruiters.Any(r => r.ModelId == request.ModelId && r.RecruiterStatus!= null && r.RecruiterStatus.Name == "Active"));
}

int totalCount = query.Count();

return (
    await query
        .AsNoTracking()
        .ProjectTo<ModelsDto>(_mapper.ConfigurationProvider)
        .OrderByDescending(t => t.Submitted)
        .Skip(request.Start)
        .Take(request.Limit)
        .ToListAsync(cancellationToken),
    totalCount);

上面的代码块返回类似这样的内容(这只是一个示例)

id : "3c1a3b03-b48a-4051-9819-961c5a9b8ed2",
created : "2022-12-08T16:54:42.428233",
submitted : "2022-12-08T16:54:42.428233",
remarks : "2022-12-08T16:54:42.428233",
recruiters : 
     [
         0 : 
              { 
                id : "08daa761-3051-4119-84da-da7c9981a631",
                modelId : "2d3c306f-5904-4ca2-b17f-1ace38f2b53e",
                agencyId : "3c1a3b03-b48a-4051-9819-961c5a9b8ed2"
                recruiterStatus :
                        {
                          name : Active
                        }
                created "2022-12-08T16:54:42.540357"
              },
         1 : 
              {
                id : "5fc94679-2b7f-411c-ac77-84f2ab289744",
                modelId : "3e49fe2f-e2fd-4435-a7cd-b1b4b89b73c1",
                agencyId : "3c1a3b03-b48a-4051-9819-961c5a9b8ed2"
                recruiterStatus :
                        {
                          name : Pending
                        }
                created "2022-12-08T16:54:42.540357"
              }
          
     ]

我查询了ModelId = 2d3c306f-5904-4ca2-b17f-1ace38f2b53e AND RecruiterStatus.Name = "Active",在JSON返回中我只希望得到1,即recruiters[0],但实际上得到了2,即recruiters[0]和recruiters[1]。

招聘人员的模型实体

public class Recruiter
{
    public Guid Id { get; set; }

    [ForeignKey("Model")]
    public Guid ModelId { get; set; }
    public Model Model { get; set; } = null!;

    [ForeignKey("Agency")]
    public Guid AgencyId { get; set; }
    public Agency Agency { get; set; } = null!;

    [ForeignKey("RecruiterStatus")]
    public Guid RecruiterStatusId { get; set; }
    public RecruiterStatus RecruiterStatus { get; set; } = null!;

    public DateTime? Created { get; set; }

机构的示范实体

public class Agency
{
    public Guid Id { get; set; }

    public DateTime Submitted { get; set; }

    public string? Remarks { get; set; } = string.Empty;

    public virtual ICollection<Recruiter> Recruiters { get; set; } = new List<Recruiter>();


有没有办法过滤(查询)出结果,只返回我要求招聘人员块?

au9on6nz

au9on6nz1#

您正在查询代理机构,ID为"3c1a3b03-b48a-4051-9819-961c5a9b8ed2"的代理机构具有您指定的两个条件。因此,当返回时,它包含所有字段,包括招聘人员(模型有2个招聘人员)。如果您想再次过滤它,您可以在内存中进行(简单方法)。这意味着您将结果放在列表中,然后为每个代理机构忽略不满足条件的项目。
简单地说,就是使用一个循环(只是代码片段):

var listOfModelsDto= await query
        .AsNoTracking()
        .ProjectTo<ModelsDto>(_mapper.ConfigurationProvider)
        .OrderByDescending(t => t.Submitted)
        .Skip(request.Start)
        .Take(request.Limit)
        .ToListAsync(cancellationToken);

foreach (var item in listOfModelsDto){
  item.Recruiters=Recruiters.where(r => r.ModelId == request.ModelId && r.RecruiterStatus!= null && r.RecruiterStatus.Name == "Active").ToList();
}

return listOfModelsDto

相关问题