当前位置:网站首页>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
边栏推荐
- Navicat connect 2002 - can't connect to local MySQL server through socket '/var/lib/mysql/mysql Sock 'solve
- Pdf document signature Guide
- [open source] Net ORM accessing Firebird database
- Ternary expressions, generative expressions, anonymous functions
- Wechat official account oauth2.0 authorizes login and displays user information
- Jerry's key to initiate pairing [chapter]
- 谈谈制造企业如何制定敏捷的数字化转型策略
- Ant destination multiple selection
- 【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式
- 如何选择合适的自动化测试工具?
猜你喜欢
谈谈制造企业如何制定敏捷的数字化转型策略
L2: current situation, prospects and pain points of ZK Rollup
Dry goods sharing | devaxpress v22.1 original help document download collection
Jerry's manual matching method [chapter]
Record a garbled code during servlet learning
DNS series (I): why does the updated DNS record not take effect?
双塔模型的最强出装,谷歌又开始玩起“老古董”了?
Embedded development: how to choose the right RTOS for the project?
Anti climbing killer
Build your own website (18)
随机推荐
How does win11 time display the day of the week? How does win11 display the day of the week today?
Jerry's key to initiate pairing [chapter]
What is the difference between the three values of null Nan undefined in JS
Song list 11111
ByteDance senior engineer interview, easy to get started, fluent
Jerry's about TWS channel configuration [chapter]
NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?
Dbsync adds support for mongodb and ES
使用 BlocConsumer 同时构建响应式组件和监听状态
Win11如何解禁键盘?Win11解禁键盘的方法
MIT6.S081-Lab9 FS [2021Fall]
How does win11 unblock the keyboard? Method of unlocking keyboard in win11
How polardb-x does distributed database hotspot analysis
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
An in-depth understanding of fp/fn/precision/recall
[colmap] sparse reconstruction is converted to mvsnet format input
Main functions of OS, Sys and random Standard Libraries
SAR影像质量评估
NVR硬盤錄像機通過國標GB28181協議接入EasyCVR,設備通道信息不顯示是什麼原因?
Code of "digital image processing principle and Practice (matlab version)" part2[easy to understand]