【问题标题】:how to Check for equality with list input in linq如何检查与linq中的列表输入是否相等
【发布时间】:2021-09-04 21:43:27
【问题描述】:

这些是我的 dtos


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


    【解决方案1】:

    根据我的理解,您有一个字符串列表,它是 TenantSections,并且您想要检索它的每个 Id 在 DTO 中的 TenantSections 内的所有租户,如果这正是您的目标那么你应该使用 .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()方法检查是否有数据返回

    【讨论】:

    • 没有访问权限检查 d 部分不起作用
    • 您能否详细解释一下 No Access 检查?
    • 我已更新答案以检查查询是否返回项目
    猜你喜欢
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多