当前位置:网站首页>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
边栏推荐
- Implementation method of data platform landing
- 【Azure微服务 Service Fabric 】如何转移Service Fabric集群中的种子节点(Seed Node)
- Use json Stringify() to realize deep copy, be careful, there may be a huge hole
- What if the win11u disk does not display? Solution to failure of win11 plug-in USB flash disk
- Google SEO external chain backlinks research tool recommendation
- How to realize the movement control of characters in horizontal game
- How to integrate Google APIs with Google's application system (1) -introduction to Google APIs
- 嵌入式开发:如何为项目选择合适的RTOS?
- Which futures company is the safest to open a futures account?
- Open source OA development platform: contract management user manual
猜你喜欢
强化学习-学习笔记9 | Multi-Step-TD-Target
使用 BlocConsumer 同时构建响应式组件和监听状态
ByteDance senior engineer interview, easy to get started, fluent
Node:504 error reporting
NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?
Use json Stringify() to realize deep copy, be careful, there may be a huge hole
Qt编写物联网管理平台39-报警联动
[开源] .Net ORM 访问 Firebird 数据库
Win11如何解禁键盘?Win11解禁键盘的方法
Paint basic graphics with custompaint
随机推荐
null == undefined
Code of "digital image processing principle and Practice (matlab version)" part2[easy to understand]
The difference between NPM uninstall and RM direct deletion
Jerry's manual matching method [chapter]
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
[azure microservice service fabric] how to transfer seed nodes in the service fabric cluster
Use json Stringify() to realize deep copy, be careful, there may be a huge hole
Jerry's about TWS pairing mode configuration [chapter]
100million single men and women "online dating", supporting 13billion IPOs
Tcp/ip protocol stack
How to quickly check whether the opening area ratio of steel mesh conforms to ipc7525
Welcome to CSDN markdown editor
Which financial products will yield high returns in 2022?
OpenGL job coordinate system
【colmap】稀疏重建转为MVSNet格式输入
Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
Validutil, "Rethinking the setting of semi supervised learning on graphs"
Tupu digital twin coal mining system to create "hard power" of coal mining
如何实现横版游戏中角色的移动控制
SAR影像质量评估