このDefaultifemptyは、文字列の代わりにIENumerable を選択するのはなぜですか?

StackOverflow https://stackoverflow.com/questions/3891374

質問

 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 文字列です。

文字列実装 IEnumerable<char>.

あなたはそれを呼んでいます Enumerable.DefaultIfEmpty メソッド、拡張 IEnumerable<T>.
この方法は、an以外のものを返すことはできません IEnumerable<T>.

編集: :交換する場合 null の値 module 非ヌル値を使用すると、ヌルコーテルオペレーターを使用できます。

group aName by module ?? "SomeValue"

でも、 module 実際には決してありません null, 、 where module!=null 句。
その後、削除する必要があります ??"Other" 決勝から select 句。

他のヒント

なぜなら、この場合、 module 文字列です:

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

文字列上のIENumerable拡張機能のいずれかを呼び出すと、 IEnumerable<char>.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top