I want to understand the complex filtering with LINQ XML. I have created a simple XML example (DataBaseCities.xml):
<?xml version="1.0" encoding="utf-8"?>
<DataBase>
<DocumentInfo version="1.0" schemaVersion="1.0"/>
<ListOfMegaCities>
<MegaCities city="Moscow" continent="Europe">
<VariantConstraint>
<LanguageRef LanguageId="russian">
<LanguageDialectsRef DialectsId="north"/>
</LanguageRef>
</VariantConstraint>
<Districts>
<CityDistrict District="Arbat"/>
<CityDistrict District="Basmanny"/>
</Districts>
</MegaCities>
<MegaCities city="New York" continent="North America">
<VariantConstraint>
<LanguageRef LanguageId="english">
<LanguageDialectsRef DialectsId="west"/>
</LanguageRef>
<LanguageRef LanguageId="spanish">
<LanguageDialectsRef DialectsId="cental"/>
</LanguageRef>
</VariantConstraint>
<Districts>
<CityDistrict District="Queens"/>
<CityDistrict District="Bronx"/>
</Districts>
</MegaCities>
<MegaCities city="London" continent="Europe">
<VariantConstraint>
<LanguageRef LanguageId="english">
<LanguageDialectsRef DialectsId="west"/>
</LanguageRef>
<LanguageRef LanguageId="spanish">
<LanguageDialectsRef DialectsId="central"/>
</LanguageRef>
<LanguageRef LanguageId="french">
<LanguageDialectsRef DialectsId="central"/>
</LanguageRef>
</VariantConstraint>
<Districts>
<CityDistrict District="Greenwich"/>
<CityDistrict District="Westminster"/>
</Districts>
</MegaCities>
</ListOfMegaCities>
</DataBase>
And I try to filter like:
XElement root = XElement.Load(@"DataBaseCities.xml");
IEnumerable<XElement> ListOfMegaCities =
from el in root.Descendants("MegaCities")
where
(from add in el.Descendants("LanguageRef")
where
(string)add.Attribute("LanguageId") == "english"
select add)
.Any()
select el;
foreach (XElement el in ListOfMegaCities)
{
Console.WriteLine((string)el.Attribute("city"));
}
So the Output is:
New York
London
But I want to filter more then one Attribute.
If I try to filter with these lines:
(string)add.Attribute("LanguageId") == "english" && (string)add.Attribute("LanguageId") == "spanish"
Why it dont work?
- How I can filter "DialectsId" too?
Example: I want to get "New York" with exactly this filtering:
LanguageId="english"
DialectsId="west"
LanguageId="spanish"
DialectsId="cental"
Maybe someone has a good source for complex filtering? I found this one https://learn.microsoft.com/de-de/dotnet/standard/linq/write-queries-complex-filtering but that helped me only partially...