当前位置:网站首页>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

原网站

版权声明
本文为[freelooppowter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130607228300.html