PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/LinqToXsdDemo/Samples/Reports.cs

#
C# | 77 lines | 61 code | 7 blank | 9 comment | 3 complexity | b9e94f03e224268ee483730d6da82ca6 MD5 | raw file
  1. //Copyright (c) Microsoft Corporation. All rights reserved.
  2. //
  3. // Two different ways of mixing typed and untyped APIs
  4. //
  5. namespace LinqToXsdDemo.Reports.Mix1
  6. {
  7. using System.Linq;
  8. using System.Xml.Linq;
  9. using Xml.Schema.Linq;
  10. using www.example.com.Company;
  11. public static class Test
  12. {
  13. static XElement CountReports(Company c)
  14. {
  15. XNamespace ns = "http://www.example.com/Company";
  16. return new XElement("Reports",
  17. from e in c.Query.Descendants<EmployeeType>()
  18. where e.Untyped.Name == ns + "Manager"
  19. select new XElement("Report",
  20. new XAttribute("Name",e.Name),
  21. (from r in ((Department)(e.Untyped.Parent))
  22. .Query.Descendants<EmployeeType>()
  23. where r != e
  24. select r
  25. ).Count()));
  26. }
  27. public static void Run()
  28. {
  29. var c = Company.Load("../../Data/Company.xml");
  30. var counts = CountReports(c);
  31. counts.CompareWithBaseline("Reports");
  32. }
  33. }
  34. }
  35. namespace LinqToXsdDemo.Reports.Mix2
  36. {
  37. using System.Linq;
  38. using System.Xml.Linq;
  39. using Xml.Schema.Linq;
  40. using www.example.com.Company;
  41. public static class Test
  42. {
  43. static XElement CountReports(Company x)
  44. {
  45. XNamespace ns = "http://www.example.com/Company";
  46. return new XElement("Reports",
  47. from euntyped in x.Untyped.Descendants(ns + "Manager")
  48. let etyped = (EmployeeType)euntyped
  49. select new XElement("Report",
  50. new XAttribute("Name",etyped.Name),
  51. (from r in ((Department)(etyped.Untyped.Parent))
  52. .Query.Descendants<EmployeeType>()
  53. where r != etyped
  54. select r
  55. ).Count()));
  56. }
  57. public static void Run()
  58. {
  59. var c = Company.Load("../../Data/Company.xml");
  60. //
  61. // The following descendants compensates for an open bug.
  62. // That is, by a typed descendants we make sure that the tree gets typed.
  63. // As a result, casts on untyped subtrees will be correct.
  64. //
  65. c.Query.Descendants<EmployeeType>().ToList();
  66. var counts = CountReports(c);
  67. counts.CompareWithBaseline("Reports");
  68. }
  69. }
  70. }