首页 > 解决方案 > 如何检查与linq中的列表输入是否相等

问题描述

这些是我的 dto


public class CreateTenantDto
{
    public Guid TenantId { get; set; }
    public List<CreateTenantSectionDto> TenantSections { get; set; }
}
public class CreateTenantSectionDto
{
    public Guid SectionId { get; set; }
    public List<CreateTenantProperties> TenantProperties { get; set; }
}
public class CreateTenantProperties
{
    public Guid PropertyId { get; set; }
    public string QuantityName { get; set; }
}

这是我使用 linq 的服务,我想用部分 id 的输入列表检查部分是否相等

public async Task<bool> AddTenant(CreateTenantDto dto)
{
  var personSectionList = await _personFormSectionDetailRepository.Query()
        .Filter(x => x.Id == ??? ).GetAllAsync();
}

标签: c#linq.net-core

解决方案


根据我的理解,您有一个字符串列表,TenantSections并且您想要检索它的每个 IdTenantSections在 DTO 中的所有租户,如果这正是您的目标,那么您应该使用 .Contains() 来实现如下:

    public async Task<bool> AddTenant(CreateTenantDto dto)
    {
        var personSectionList = await _personFormSectionDetailRepository.Query()
                .Filter(x => dto.TenantSections.Contains(x.Id)).GetAllAsync();
      return personSectionList.Any();
    }

.Contains()这种情况下,检查租户的每个 Id 是否实际包含在参数中提供的列表中

更新:

我已经更新了上面的代码以添加检查是否有数据返回或没有使用该.Any()方法


推荐阅读