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( ) };
Asked
Active
Viewed 957 times
1

Maslow
- 18,464
- 20
- 106
- 193
-
if it is not clear, I have a list of assemblies from an xml file, where I want to see how many are in an `interested modules` namespace along with the `interestedModules` name or other if it's not one i'm particularly interested in – Maslow Oct 08 '10 at 14:10
-
What are you expecting it to return? – SLaks Oct 08 '10 at 14:10
-
Why are you calling it at all? – SLaks Oct 08 '10 at 14:12
-
because I don't want the group by filtering out results where module is null – Maslow Oct 08 '10 at 14:15
-
Then why do you have `where module!=null`? See my edit. – SLaks Oct 08 '10 at 14:18
2 Answers
4
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.

SLaks
- 868,454
- 176
- 1,908
- 1,964
1
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>
.

Justin Niessner
- 242,243
- 40
- 408
- 536