c# - 如何检查与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();
}
解决方案
根据我的理解,您有一个字符串列表,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()
方法
推荐阅读
- java - 将 int 数组从 Java 传递到 Oracle PlSql
- typo3 - 安装时出现typo3错误,没有可用的错误日志
- alexa-skills-kit - 如何设置默认意图?
- python - CPLEX 和 Python 3.7
- sql - 为什么在having子句中使用别名有效?
- git - 是否可以在 NPM 中停用 ls-remote 检查?
- spring - 如何在 application.properties 中表示 Map - Spring Boot
- sql - SQL Group By最小值可用作变量?
- react-native - 我可以将 Web 应用程序的组件与 React Native 应用程序一起使用吗
- scala - Scala案例类中的隐式与隐式val