List<T>Find方法,FindAll方法,Contains方法,Equals方法

本文介绍了C#中处理字符串查询的几种方法,包括精确查询equals方法、模糊查询Contains方法及FindAll方法的应用,并对比了Contains与IndexOf的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

假如传入的T是一个类,

List<MessageInfos> MessageInfos = new List<MessageInfos>();

MessageInfos= MessageInfos.FindAll(tmp => tmp.title.Contains(txt_title.Text.Trim()) || tmp.content.Contains(txt_content.Text.Trim()) || tmp.buyerMobile.Contains(txt_search.Text.Trim()));满足其中一个条件即可

MessageInfos= MessageInfos.FindAll(tmp => tmp.title.Contains(txt_title.Text.Trim()) && tmp.content.Contains(txt_content.Text.Trim()) && tmp.buyerMobile.Contains(txt_search.Text.Trim()));满足所有条件

1、equals 方法:(是String类从它的超类Object中继承的)被用来检测两个对象是否相等,即两个对象的内容是否相等,区分大小写。有逐个对比的意思(精确查询)

2、Contains方法:(相当于模糊查询)

C#中要判断一个字符串是否包含另一个字符串,常用的两种方法是:
str.Contains和str.IndexOf
这两个方法的区别是:

Contains是找指定字符串是否包含一个字串,返回值的boolean类型,即只有true和false

IndexOf有多个重载,但无论哪个,都是做一定的匹配,然后把匹配的第一个字符的位置返回,返回的是int类型,如果没找到,那么返回-1

public bool Contains(string value)
{
  return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
}
3、find方法:返回的是一个结果
4、findAll方法:返回的是多个结果

转载于:https://www.cnblogs.com/candyzhmm/p/6024423.html

List<ConflictInfo> list = conflictInfoMapper.findPage(pagination, queryDTO); //获取登记人员id集合 List<Integer> registerUserIdList = new ArrayList<>(); //查询全部纠纷类型 List<ConflictApplicationType> conflictTypes = typeService.findAll(); list.forEach(conflictInfo ->{ Integer registerUserId = conflictInfo.getVisitPerson().contains(",") ? Integer.valueOf(conflictInfo.getVisitPerson().substring(0, conflictInfo.getVisitPerson().indexOf(","))) : Integer.valueOf(conflictInfo.getVisitPerson()); registerUserIdList.add(registerUserId); }); //获取登记人员手机号集合 List<Map<Integer,String>> registerUserPhoneMapList = conflictRegisterUserMapper.findRegisterUserPhoneMap(registerUserIdList); //根据纠纷来源类型查询纠纷来源类型字典对象集合 List<SysCode> conflictSourceCodeList = codeService.findByCodeType("CONFLICT_SOURCE"); //根据矛盾状态类型查询矛盾状态类型字典对象集合 List<SysCode> conflictStatusCodeList = codeService.findByCodeType("CONFLICT_STATUS"); list.forEach(conflictInfo -> { Integer registerUserId = conflictInfo.getVisitPerson().contains(",") ? Integer.valueOf(conflictInfo.getVisitPerson().substring(0, conflictInfo.getVisitPerson().indexOf(","))) : Integer.valueOf(conflictInfo.getVisitPerson()); conflictInfo.setVisitPersonName(convertName(conflictInfo.getVisitPerson(), ",", "")); //设置手机号 Optional<Map<Integer,String>> mapOptional = registerUserPhoneMapList.stream().filter(e->String.valueOf(e.get("id")).equals(registerUserId.toString())).findFirst(); mapOptional.ifPresent(e->conflictInfo.setPhoneNo(e.get("phoneNo"))); //设置矛调来源名称 Optional<SysCode> sourceCodeOptional = conflictSourceCodeList.stream().filter(e->e.getCodeValue().equals(conflictInfo.getConflictSource())).findFirst(); sourceCodeOptional.ifPresent(e->conflictInfo.setConflictSourceName(e.getCodeName())); //设置矛调状态名称 Optional<SysCode> statusCodeOptional = conflictStatusCodeList.stream().filter(e->e.getCodeValue().equals(conflictInfo.getConflictStatus())).findFirst(); statusCodeOptional.ifPresent(e->conflictInfo.setConflictStatusName(e.getCodeName())); //设置纠纷类型名称 Optional<ConflictApplicationType> conflictTypeOptional= conflictTypes.stream().filter(e->e.getId().toString().equals(conflictInfo.getConflictType())).findFirst(); conflictTypeOptional.ifPresent(e->conflictInfo.setConflictTypeName(e.getDisputeType())); if (StringUtils.isBlank(queryDTO.getListPattern())) { if(null != UserThreadContext.getCurrentThreadUser()){ String taskId = ActivityUtils.findGroupTaskByProcessInstanceId(UserThreadContext.getCurrentThreadUser().getDeptId().toString(), conflictInfo.getProcessId()).get(0).getTaskId(); ConflictFlowPath flowPath = flowPathService.selectOneByTaskId(taskId); if (Objects.nonNull(flowPath)) { conflictInfo.setFlowPathId(flowPath.getId()); } } } //增加部门code转名称以及受理人姓名 }); 优化一下
03-22
PageQuery<ConflictInfo> page = new PageQuery<>(queryDTO.getCurrent(), queryDTO.getSize()); List<ConflictInfo> list = conflictInfoMapper.findPage(pagination, queryDTO); //获取登记人员id集合 List<Integer> registerUserIdList = new ArrayList<>(); //查询全部纠纷类型 List<ConflictApplicationType> conflictTypes = typeService.findAll(); list.forEach(conflictInfo ->{ Integer registerUserId = conflictInfo.getVisitPerson().contains(",") ? Integer.valueOf(conflictInfo.getVisitPerson().substring(0, conflictInfo.getVisitPerson().indexOf(","))) : Integer.valueOf(conflictInfo.getVisitPerson()); registerUserIdList.add(registerUserId); }); //获取登记人员手机号集合 List<Map<Integer,String>> registerUserPhoneMapList = conflictRegisterUserMapper.findRegisterUserPhoneMap(registerUserIdList); //根据纠纷来源类型查询纠纷来源类型字典对象集合 List<SysCode> conflictSourceCodeList = codeService.findByCodeType("CONFLICT_SOURCE"); //根据矛盾状态类型查询矛盾状态类型字典对象集合 List<SysCode> conflictStatusCodeList = codeService.findByCodeType("CONFLICT_STATUS"); list.forEach(conflictInfo -> { Integer registerUserId = conflictInfo.getVisitPerson().contains(",") ? Integer.valueOf(conflictInfo.getVisitPerson().substring(0, conflictInfo.getVisitPerson().indexOf(","))) : Integer.valueOf(conflictInfo.getVisitPerson()); conflictInfo.setVisitPersonName(convertName(conflictInfo.getVisitPerson(), ",", "")); //设置手机号 Optional<Map<Integer,String>> mapOptional = registerUserPhoneMapList.stream().filter(e->String.valueOf(e.get("id")).equals(registerUserId.toString())).findFirst(); mapOptional.ifPresent(e->conflictInfo.setPhoneNo(e.get("phoneNo"))); //设置矛调来源名称 Optional<SysCode> sourceCodeOptional = conflictSourceCodeList.stream().filter(e->e.getCodeValue().equals(conflictInfo.getConflictSource())).findFirst(); sourceCodeOptional.ifPresent(e->conflictInfo.setConflictSourceName(e.getCodeName())); //设置矛调状态名称 Optional<SysCode> statusCodeOptional = conflictStatusCodeList.stream().filter(e->e.getCodeValue().equals(conflictInfo.getConflictStatus())).findFirst(); statusCodeOptional.ifPresent(e->conflictInfo.setConflictStatusName(e.getCodeName())); //设置纠纷类型名称 Optional<ConflictApplicationType> conflictTypeOptional= conflictTypes.stream().filter(e->e.getId().toString().equals(conflictInfo.getConflictType())).findFirst(); conflictTypeOptional.ifPresent(e->conflictInfo.setConflictTypeName(e.getDisputeType())); if (StringUtils.isBlank(queryDTO.getListPattern())) { if(null != UserThreadContext.getCurrentThreadUser()){ String taskId = ActivityUtils.findGroupTaskByProcessInstanceId(UserThreadContext.getCurrentThreadUser().getDeptId().toString(), conflictInfo.getProcessId()).get(0).getTaskId(); ConflictFlowPath flowPath = flowPathService.selectOneByTaskId(taskId); if (Objects.nonNull(flowPath)) { conflictInfo.setFlowPathId(flowPath.getId()); } } } //增加部门code转名称以及受理人姓名 }); if (list.isEmpty()) { return page; } page.setRecords(list); page.setTotal(pagination.getTotal()); return page; 优化一下这段java代码
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值