Linq to xml LINQXML包含在where子句中
地图和基站都是xml文件。我被困在//位置(geoSettings.Location.Contains(x.Element(“Location”).Value)) geoSettings是一个IEnumerable。如何获取“位置”?使用联接Linq to xml LINQXML包含在where子句中,linq-to-xml,Linq To Xml,地图和基站都是xml文件。我被困在//位置(geoSettings.Location.Contains(x.Element(“Location”).Value)) geoSettings是一个IEnumerable。如何获取“位置”?使用联接 var geoSettings = (from c in geoFields.Elements("Maps").Elements("Map") select new
var geoSettings = (from c in geoFields.Elements("Maps").Elements("Map")
select new
{
loc = c.Element("Location").Value
}).Distinct().Intersect(from p in terrainFields.Elements("Maps").Elements("Map")
select new
{
loc = p.Element("Location").Value
});
var flightCheck = from x in baseStations.Elements("BaseStation").Elements("Station")
// where (geoSettings.Location.Contains(x.Element("Location").Value))
select new
{
Flights = x.Element("FlightName").Value,
loc = x.Element("Location").Value
};
这将只提供来自x的元素,其中y中有匹配的元素。
geoSettings
是一个IEnumerable<代码>地理设置。loc不是。(我假定您的意思是loc
,而不是Location
,因为您的代码不包含后者…)
所以您可以进一步理解我的意思,注意geoSettings[n].loc
是有效的,但是geoSettings.loc[n]
不是
因此…您需要使用Any()
而不是Contains()
,如“如果我的geoSettings元素集合中有匹配的位置”:
旁注:如果您确实只在
geoSettings
中选择一个值,而不是将其设置为匿名类型的集合(使用单个属性loc
),只需将其设置为字符串的集合并仅选择该值即可。然后,您可以使用Contains()
并保存以生成一大堆新对象。无法访问地理设置。位置是这样的,因为地理设置是IEnumerableI used joins类型,并且它工作正常,感谢您的想法,但仍然是地理设置。位置不正确,因此我无法将您的响应标记为正确答案。
var flightCheck = from x in baseStations.Elements("BaseStation").Elements("Station")
join y in geoSettings.Location on x x.Element("Location").Value equals y.Value
select new
{
Flights = x.Element("FlightName").Value,
loc = x.Element("Location").Value
};
var flightCheck = from x in baseStations.Elements("BaseStation").Elements("Station")
where geoSettings.Any(geo => geo.loc.Contains(x.Element("Location").Value)
select new
{
Flights = x.Element("FlightName").Value,
loc = x.Element("Location").Value
};