tags:

views:

19

answers:

2
 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
                         };

Both Maps and BaseStation are xml files.I'm stuck at // where(geoSettings.Location.Contains(x.Element("Location").Value)) geoSettings is an IEnumerable.How can i get the "Location"?

A: 

Use a join instead

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
                  };

That will give you only elements from x where there is a matching element in y.

Ben Robinson
Cant access geoSettings.Location this way because geoSettings is of type IEnumerable
Webbies
I used joins and it worked , thanks for the idea but still geoSettings.Location is incorrect so i'm unable to tag your response as the right answer.
Webbies
A: 

geoSettings is an IEnumerable. geoSettings.loc isn't. (I assume you mean loc, not Location, because your code doesn't contain the latter...)

So you can further understand what I mean, note that geoSettings[n].loc is valid, but geoSettings.loc[n] is not.

So...you're going to need to use Any() instead of Contains(), as in "if anything in my collection of geoSettings elements has a matching location":

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
                  };

Side note: if you really are only selecting one value into geoSettings, instead of making it a collection of anonymous types (with a single property loc), just make it a collection of strings and select only the value. Then you can use Contains() and save making a whole bunch of new objects.

lc