Guest User

Untitled

a guest
Jun 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. string location = "Oslo";
  2. var training = (from item in doc.Descendants("item")
  3. where item.Value.Contains(location)
  4.  
  5. select new
  6. {
  7. event = item.Element("event").Value,
  8. event_location = item.Element("location").Value
  9. }).ToList();
  10.  
  11. <training>
  12. <item>
  13. <event>C# Training</event>
  14. <location>Prague</location>
  15. <location>Oslo</location>
  16. <location>Amsterdam</location>
  17. <location>Athens</location>
  18. <location>Dublin</location>
  19. <location>Helsinki</location>
  20. </item>
  21. <item>
  22. <event>LINQ Training</event>
  23. <location>Bucharest</location>
  24. <location>Oslo</location>
  25. <location>Amsterdam</location>
  26. <location>Helsinki</location>
  27. <location>Brussels</location>
  28. <location>Dublin</location>
  29. </item>
  30. </training>
  31.  
  32. string location = "Oslo";
  33. var training = from loc in doc.Descendants("location")
  34. where loc.Value == location
  35. select new
  36. {
  37. event = loc.Parent.Element("event").Value,
  38. event_location = loc.Value
  39. };
  40.  
  41. string location = "Oslo";
  42. var training = from loc in doc.Descendants("location")
  43. where loc.Value == location
  44. select new
  45. {
  46. event = loc.Parent.Element("event").Value,
  47. event_locations = loc.Parent.Elements("location")
  48. .Select(e => e.Value)
  49. };
  50.  
  51. for (var entry in training)
  52. {
  53. Console.WriteLine("Event: {0}; Locations: {1}",
  54. entry.event,
  55. string.Join(", ", entry.event_locations.ToArray());
  56. }
  57.  
  58. var training = (from item in root.Descendants("item")
  59. where item.Value.Contains(location)
  60. select new
  61. {
  62. name = item.Element("event").Value,
  63. location = (from node in item.Descendants("location")
  64. where node.Value.Equals(location)
  65. select node.Value).FirstOrDefault(),
  66. }).ToList();
Add Comment
Please, Sign In to add comment