当前位置:网站首页>Remember an experience of using selectmany
Remember an experience of using selectmany
2022-07-07 22:18:00 【freelooppowter】
Recently, when transforming a function, in order to reduce the number of layers of circulation , So I thought I would List The list is mapped to a list that can directly use the particle size List list , Such a loop can solve the problem .
public class ConflictWordItemForDisplay { /// <summary> /// Datum field /// </summary> public string BasisField { get; set; } /// <summary> /// keyword /// </summary> public string Keyword { get; set; } /// <summary> /// Conflicting words ( Support multiple words , Use... Between words , Separate ) /// </summary> public string ConflictWord { get; set; } /// <summary> /// Discrimination field /// </summary> public List<string> JudgingFields { get; set; } }
The defined data class is similar to the above , It may be necessary to use a three-tier loop in actual use to make judgment , Similar to the following pseudo code
foreach (var item in conflictWordItemForDisplayList) { if ( The contents of the reference field .ToUpper().contains(item.Keyword.ToUpper())) { foreach (var judgingField in item.JudgingFields) { foreach (var word in item.ConflictWord.Split(',')) { if (judgingField The content of the corresponding discrimination field .ToUpper().contains(word.ToUpper())) { // Conflict , Carry out corresponding treatment ( Prompt the user to let the user modify and so on ) } } } } }
When the discrimination field types are few and fixed, the judgment logic can be used to reduce one layer of circulation .
If you use SelectMany How to minimize the granularity . Microsoft SelectMany The description of is to project each element of the sequence onto IEnumerable<T> And merge the result sequence into one sequence . Include the following overloads
Reduce the discrimination field to the minimum granularity :
var allConflictWordItemForDisplay = conflictWordItemForDisplayList.SelectMany(p => p.JudgingFields, (s, r) => new ConflictWordItemForDisplay() { BasisField=s.BasisField,Keyword=s.Keyword,ConflictWord=s.ConflictWord,JudgingFields=new List<string> { r} });
Reduce conflicting words to the granularity of a single word :
allConflictWordItemForDisplay = allConflictWordItemForDisplay.SelectMany(p => p.ConflictWord.Split(','), (s, r) => new ConflictWordItemForDisplay() { BasisField = s.BasisField, Keyword = s.Keyword, ConflictWord = r, JudgingFields = s.JudgingFields });
This is just SelectMany The way I use this question ,SelectMany There are other overloaded functions , When you use it, you can learn more about .
The following is Microsoft's official document , There are also use cases for easy understanding .
https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable.selectmany?view=netframework-4.6.1
边栏推荐
- Jerry's power on automatic pairing [chapter]
- How to quickly check whether the opening area ratio of steel mesh conforms to ipc7525
- Navicat connect 2002 - can't connect to local MySQL server through socket '/var/lib/mysql/mysql Sock 'solve
- OpeGL personal notes - lights
- Google SEO external chain backlinks research tool recommendation
- Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
- Win11如何解禁键盘?Win11解禁键盘的方法
- Ad domain group policy management
- Interview question 01.02 Determine whether it is character rearrangement - auxiliary array algorithm
- Implementation method of data platform landing
猜你喜欢
ByteDance Android interview, summary of knowledge points + analysis of interview questions
海外代理推荐
[advanced MySQL] index details (I): index data page structure
【JDBC Part 1】概述、获取连接、CRUD
【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
Win11如何解禁键盘?Win11解禁键盘的方法
Jenkins user rights management
Win11游戏模式怎么开启?Win11开启游戏模式的方法
The function is really powerful!
强化学习-学习笔记9 | Multi-Step-TD-Target
随机推荐
Backup tidb cluster to persistent volume
Jerry's about TWS channel configuration [chapter]
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
[open source] Net ORM accessing Firebird database
2022 how to evaluate and select low code development platforms?
OpenGL job - texture
Two kinds of updates lost and Solutions
#DAYU200体验官#MPPT光伏发电项目 DAYU200、Hi3861、华为云IotDA
The maximum number of meetings you can attend [greedy + priority queue]
怎样写一个增广矩阵到txt文件中
How to make agile digital transformation strategy for manufacturing enterprises
Typescript TS basic knowledge type declaration
L2:ZK-Rollup的现状,前景和痛点
[开源] .Net ORM 访问 Firebird 数据库
Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
How to write an augmented matrix into TXT file
DBSync新增对MongoDB、ES的支持
The free styling service of Dyson's official direct store is now open for appointment. Pioneer Technology interprets the styling concept of hair care and helps consumers unlock diversified and shiny s
Ad domain group policy management
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设