当前位置:网站首页>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
边栏推荐
- Open source OA development platform: contract management user manual
- DNS series (I): why does the updated DNS record not take effect?
- 大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
- Use br to recover backup data on azure blob storage
- Jerry's key to initiate pairing [chapter]
- Win11U盘不显示怎么办?Win11插U盘没反应的解决方法
- Time standard library
- [JDBC Part 1] overview, get connection, CRUD
- NVR硬盤錄像機通過國標GB28181協議接入EasyCVR,設備通道信息不顯示是什麼原因?
- 强化学习-学习笔记9 | Multi-Step-TD-Target
猜你喜欢
谈谈制造企业如何制定敏捷的数字化转型策略
Matplotlib drawing interface settings
Talk about relational database and serverless
Pdf document signature Guide
The whole network "chases" Zhong Xuegao
如何选择合适的自动化测试工具?
Validutil, "Rethinking the setting of semi supervised learning on graphs"
【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式
Reptile combat (VII): pictures of the king of reptiles' heroes
operator
随机推荐
DNS series (I): why does the updated DNS record not take effect?
Main functions of OS, Sys and random Standard Libraries
OpenGL jobs - shaders
Wechat official account oauth2.0 authorizes login and displays user information
Restore backup data on persistent volumes
Which financial products will yield high returns in 2022?
TCP/IP 协议栈
Reptile combat (VII): pictures of the king of reptiles' heroes
Dry goods sharing | devaxpress v22.1 original help document download collection
[open source] Net ORM accessing Firebird database
[colmap] sparse reconstruction is converted to mvsnet format input
What is the difference between the three values of null Nan undefined in JS
Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
The whole network "chases" Zhong Xuegao
Qt编写物联网管理平台39-报警联动
Restapi version control strategy [eolink translation]
Tsconfig of typescript TS basics JSON configuration options
Use br to recover backup data on azure blob storage
2022 how to evaluate and select low code development platforms?
谈谈制造企业如何制定敏捷的数字化转型策略