In a previous question I asked about how to group XML elements logically, and I got the answer, which was to nest the Linq query.
Problem is, this has the effect of left-joining the nested queries. For example, let's say I want to list all the cities in the USA that begin with the letter "Y", grouped by State and County:
XElement xml = new XElement("States",
from s in LinqUtils.GetTable<State>()
orderby s.Code
select new XElement("State",
new XAttribute("Code", s.Code),
new XAttribute("Name", s.Name),
from cy in s.Counties
orderby cy.Name
select new XElement("County",
new XAttribute("Name", cy.Name),
from c in cy.Cities
where c.Name.StartsWith("Y")
orderby c.Name
select new XElement("City",
new XAttribute("Name", c.Name)
)
)
)
);
Console.WriteLine(xml);
This outputs:
<States>
<State Code="AK" Name="Alaska ">
<County Name="ALEUTIANS EAST" />
<County Name="ALEUTIANS WEST" />
<County Name="ANCHORAGE" />
<County Name="BETHEL" />
...
<County Name="YAKUTAT">
<City Name="YAKUTAT" />
</County>
<County Name="YUKON KOYUKUK" />
</State>
<State Code="AL" Name="Alabama ">
<County Name="AUTAUGA" />
...
etc.
I don't want the left-join effect; I only want to see the states and counties that actually contain cities beginning with the letter "Y".
I can think of a few ways to do this, but they all seem kludgy and inelegant. What is the neatest way you can think of to achieve the desired effect?