开发者

Why is this defaultIfEmpty choosing ienumerable<char> instead of string?

开发者 https://www.devze.com 2023-01-19 18:45 出处:网络
from a in mainDoc.XPathSelectElements开发者_运维技巧(\"//AssembliesMetrics/Assembly/@Assembly\")
 from a in mainDoc.XPathSelectElements开发者_运维技巧("//AssembliesMetrics/Assembly/@Assembly")
                            let aVal=a.Value
                            where aVal.IsNullOrEmpty( )==false&&aVal.Contains(" ")
                            select aVal.Substring(0, aVal.IndexOf(' '))
                              into aName
                              let interestedModules=new[ ] { "Core", "Credit", "Limits", "Overdraft" }
                              where aName.Contains(".")
                              let module=
                                interestedModules
                                .FirstOrDefault(x => aName
                                  .StartsWith(x, StringComparison.InvariantCultureIgnoreCase))
                              where module!=null
                              group aName by module.DefaultIfEmpty() // ienumerable<char>, why?
                                into groups
                                select new { Module=groups.Key??"Other", Count=groups.Count( ) };


module is a string.

String implements IEnumerable<char>.

You're calling the Enumerable.DefaultIfEmpty method, which extends IEnumerable<T>.
This method can never return anything other than an IEnumerable<T>.

EDIT: If you want to replace null values of module with a non-null value, you can use the null-coalescing operator:

group aName by module ?? "SomeValue"

However, module will never actually be null, because of the where module!=null clause.
You should then also remove ??"Other" from the final select clause.


Because, in this case, module is a string:

let module = interestedModules
                 .FirstOrDefault(x => aName
                 .StartsWith(x, StringComparison.InvariantCultureIgnoreCase))

When you call any of the IEnumerable extensions on a string, it decomposes into an IEnumerable<char>.

0

精彩评论

暂无评论...
验证码 换一张
取 消