• Linq编程101例


      原文地址:101 LINQ Samples in C#

    1. Part1 - Restriction Operators
    2. Part2 - Projection Operators
    3. Part3 - Partitioning Operators
    4. Part4 - Ordering Operators
    5. Part5 - Grouping Operators
    6. Part6 - Set Operators
    7. Part7 - Conversion Opertions
    8. Part8 - Element
    9. Part9 - Generation
    10. Part10 - Quantifiers
    11. Part11 - Aggregate
    12. Part12 - Miscellaneous
    13. Part13 - CustomSequence
    14. Part14 - QueryExecution
    15. Part15 - Join

      点我下载代码

    • Program.cs
      1 using System;
      2 
      3 namespace Linq101
      4 {
      5     public class Program
      6     {
      7         private static void Main()
      8         {
      9             #region Restriction
     10             //Restriction restriction = new Restriction();
     11             //restriction.Simple1();//Where - Simple 1
     12             //restriction.Simple2();//Where - Simple 2
     13             //restriction.Simple3();//Where - Simple 3
     14             //restriction.Simple4();//Where - Drilldown
     15             //restriction.Simple5();//Where - Indexed
     16             #endregion
     17 
     18             #region Projection
     19             //Projection projection = new Projection();
     20             //projection.Linq6();//Select - Simple 1
     21             //projection.Linq7();//Select - Simple 2
     22             //projection.Linq8();//Select - Transformation
     23             //projection.Linq9();//Select - Anonymous Types 1
     24             //projection.Linq10();//Select - Anonymous Types 2
     25             //projection.Linq11();//Select - Anonymous Types 3
     26             //projection.Linq12();//Select - Indexed
     27             //projection.Linq13();//Select - Filtered
     28             //projection.Linq14();//SelectMany - Compound from 1
     29             //projection.Linq15();//SelectMany - Compound from 2
     30             //projection.Linq16();//SelectMany - Compound from 3
     31             //projection.Linq17();//SelectMany - from Assignment
     32             //projection.Linq18();//SelectMany - Multiple from
     33             //projection.Linq19();//SelectMany - Indexed
     34             #endregion
     35 
     36             #region Partitioning
     37             //Partitioning partitioning = new Partitioning();
     38             //partitioning.Linq20();//Take - Simple
     39             //partitioning.Linq21();//Take - Nested
     40             //partitioning.Linq22();//Skip - Simple
     41             //partitioning.Linq23();//Skip - Nested
     42             //partitioning.Linq24();//TakeWhile - Simple
     43             //partitioning.Linq25();//TakeWhile - Indexed
     44             //partitioning.Linq26();//SkipWhile - Simple
     45             //partitioning.Linq27();//SkipWhile - Indexed
     46             #endregion
     47 
     48             #region Ordering
     49             //Ordering ordering = new Ordering();
     50             //ordering.Linq28();//OrderBy - Simple 1
     51             //ordering.Linq29();//OrderBy - Simple 2
     52             //ordering.Linq30();//OrderBy - Simple 3
     53             //ordering.Linq31();//OrderBy - Comparer
     54             //ordering.Linq32();//OrderByDescending - Simple 1
     55             //ordering.Linq33();//OrderByDescending - Simple 2
     56             //ordering.Linq34();//OrderByDescending - Comparer
     57             //ordering.Linq35();//ThenBy - Simple
     58             //ordering.Linq36();//ThenBy - Comparer
     59             //ordering.Linq37();//ThenByDescending - Simple
     60             //ordering.Linq38();//ThenByDescending - Comparer
     61             //ordering.Linq39();//Reverse
     62             #endregion
     63 
     64             #region Grouping
     65             //Grouping grouping = new Grouping();
     66             //grouping.Linq40();//GroupBy - Simple 1
     67             //grouping.Linq41();//GroupBy - Simple 2
     68             //grouping.Linq42();//GroupBy - Simple 3
     69             //grouping.Linq43();//GroupBy - Nested
     70             //grouping.Linq44();//GroupBy - Comparer
     71             //grouping.Linq45();//GroupBy - Comparer, Mapped
     72             #endregion
     73 
     74             #region Set
     75             //Set set = new Set();
     76             //set.Linq46();//Distinct - 1
     77             //set.Linq47();//Distinct - 2
     78             //set.Linq48();//Union - 1
     79             //set.Linq49();//Union - 2
     80             //set.Linq50();//Intersect - 1
     81             //set.Linq51();//Intersect - 2
     82             //set.Linq52();//Except - 1
     83             //set.Linq53();//Except - 2
     84             #endregion
     85 
     86             #region Conversion
     87             //Conversion conversion=new Conversion();
     88             //conversion.Linq54();//ToArray
     89             //conversion.Linq55();//ToList
     90             //conversion.Linq56();//ToDictionary
     91             //conversion.Linq57();//OfType
     92             #endregion
     93 
     94             #region Element
     95             //Element element=new Element();
     96             //element.Linq58();//First - Simple
     97             //element.Linq59();//First - Condition
     98             //element.Linq60();//FirstOrDefault - Simple
     99             //element.Linq61();//FirstOrDefault - Condition
    100             //element.Linq62();//ElementAt
    101             #endregion
    102 
    103             #region Generation
    104             //Generation generation = new Generation();
    105             //generation.Linq65();//Range
    106             //generation.Linq66();//Repeat
    107             #endregion
    108 
    109             #region Quantifiers
    110             //Quantifiers quantifiers=new Quantifiers();
    111             //quantifiers.Linq67();//Any - Simple
    112             //quantifiers.Linq69();//All - Grouped
    113             //quantifiers.Linq70();//All - Simple
    114             //quantifiers.Linq72();//All - Grouped
    115             #endregion
    116 
    117             #region Aggregate
    118             //Aggregate aggregate = new Aggregate();
    119             //aggregate.Linq73();//Count - Simple
    120             //aggregate.Linq74();//Count - Conditional
    121             //aggregate.Linq76();//Count - Nested
    122             //aggregate.Linq77();//Count - Grouped
    123             //aggregate.Linq78();//Sum - Simple
    124             //aggregate.Linq79();//Sum - Projection
    125             //aggregate.Linq80();//Sum - Grouped
    126             //aggregate.Linq81();//Min - Simple
    127             //aggregate.Linq82();//Min - Projection
    128             //aggregate.Linq83();//Min - Grouped
    129             //aggregate.Linq84();//Min - Elements
    130             //aggregate.Linq85();//Max - Simple
    131             //aggregate.Linq86();//Max - Projection
    132             //aggregate.Linq87();//Max - Grouped
    133             //aggregate.Linq88();//Max - Elements
    134             //aggregate.Linq89();//Average - Simple
    135             //aggregate.Linq90();//Average - Projection
    136             //aggregate.Linq91();//Average - Grouped
    137             //aggregate.Linq92();//Aggregate - Simple
    138             //aggregate.Linq93();//Aggregate - Seed
    139             #endregion
    140 
    141             #region Miscellaneous
    142             //Miscellaneous miscellaneous=new Miscellaneous();
    143             //miscellaneous.Linq94();//Concat - 1
    144             //miscellaneous.Linq95();//Concat - 2
    145             //miscellaneous.Linq96();//EqualAll - 1
    146             //miscellaneous.Linq97();//EqualAll - 2
    147             #endregion
    148 
    149             #region CustomSequence
    150             //CustomSequence customSequence =new CustomSequence();
    151             //customSequence.Linq98();//Combine         
    152             #endregion
    153 
    154             #region QueryExecution
    155             //QueryExecution queryExecution = new QueryExecution();
    156             //queryExecution.Linq99();//Deferred Execution
    157             //queryExecution.Linq100();//Immediate Execution
    158             //queryExecution.Linq101();//Query Reuse
    159             #endregion
    160 
    161             #region Join
    162             Join join=new Join();
    163             //join.Linq102();//Cross Join
    164             //join.Linq103();//Group Join
    165             //join.Linq104();//Cross Join with Group Join
    166             //join.Linq105();//Left Outer Join
    167             #endregion
    168             Console.ReadLine();
    169         }
    170     }
    171 }
    • Data.cs
      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Xml.Linq;
      5 
      6 namespace Linq101
      7 {
      8     class Data
      9     {
     10         public class Product
     11         {
     12             public int ProductID { get; set; }
     13             public string ProductName { get; set; }
     14             public string Category { get; set; }
     15             public decimal UnitPrice { get; set; }
     16             public int UnitsInStock { get; set; }
     17         }
     18 
     19         public class Order
     20         {
     21             public int OrderID { get; set; }
     22             public DateTime OrderDate { get; set; }
     23             public decimal Total { get; set; }
     24         }
     25 
     26         public class Customer
     27         {
     28             public string CustomerID { get; set; }
     29             public string CompanyName { get; set; }
     30             public string Address { get; set; }
     31             public string City { get; set; }
     32             public string Region { get; set; }
     33             public string PostalCode { get; set; }
     34             public string Country { get; set; }
     35             public string Phone { get; set; }
     36             public string Fax { get; set; }
     37             public Order[] Orders { get; set; }
     38         }
     39 
     40         private static List<Product> productList;
     41         private static List<Customer> customerList;
     42 
     43         public static List<Product> GetProductList()
     44         {
     45             if (productList == null)
     46                 CreateLists();
     47 
     48             return productList;
     49         }
     50 
     51         public static List<Customer> GetCustomerList()
     52         {
     53             if (customerList == null)
     54                 CreateLists();
     55 
     56             return customerList;
     57         }
     58 
     59         private static void CreateLists()
     60         {
     61             // Product data created in-memory using collection initializer:
     62             productList =
     63                 new List<Product> {
     64                     new Product { ProductID = 1, ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 39 },
     65                     new Product { ProductID = 2, ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = 17 },
     66                     new Product { ProductID = 3, ProductName = "Aniseed Syrup", Category = "Condiments", UnitPrice = 10.0000M, UnitsInStock = 13 },
     67                     new Product { ProductID = 4, ProductName = "Chef Anton's Cajun Seasoning", Category = "Condiments", UnitPrice = 22.0000M, UnitsInStock = 53 },
     68                     new Product { ProductID = 5, ProductName = "Chef Anton's Gumbo Mix", Category = "Condiments", UnitPrice = 21.3500M, UnitsInStock = 0 },
     69                     new Product { ProductID = 6, ProductName = "Grandma's Boysenberry Spread", Category = "Condiments", UnitPrice = 25.0000M, UnitsInStock = 120 },
     70                     new Product { ProductID = 7, ProductName = "Uncle Bob's Organic Dried Pears", Category = "Produce", UnitPrice = 30.0000M, UnitsInStock = 15 },
     71                     new Product { ProductID = 8, ProductName = "Northwoods Cranberry Sauce", Category = "Condiments", UnitPrice = 40.0000M, UnitsInStock = 6 },
     72                     new Product { ProductID = 9, ProductName = "Mishi Kobe Niku", Category = "Meat/Poultry", UnitPrice = 97.0000M, UnitsInStock = 29 },
     73                     new Product { ProductID = 10, ProductName = "Ikura", Category = "Seafood", UnitPrice = 31.0000M, UnitsInStock = 31 },
     74                     new Product { ProductID = 11, ProductName = "Queso Cabrales", Category = "Dairy Products", UnitPrice = 21.0000M, UnitsInStock = 22 },
     75                     new Product { ProductID = 12, ProductName = "Queso Manchego La Pastora", Category = "Dairy Products", UnitPrice = 38.0000M, UnitsInStock = 86 },
     76                     new Product { ProductID = 13, ProductName = "Konbu", Category = "Seafood", UnitPrice = 6.0000M, UnitsInStock = 24 },
     77                     new Product { ProductID = 14, ProductName = "Tofu", Category = "Produce", UnitPrice = 23.2500M, UnitsInStock = 35 },
     78                     new Product { ProductID = 15, ProductName = "Genen Shouyu", Category = "Condiments", UnitPrice = 15.5000M, UnitsInStock = 39 },
     79                     new Product { ProductID = 16, ProductName = "Pavlova", Category = "Confections", UnitPrice = 17.4500M, UnitsInStock = 29 },
     80                     new Product { ProductID = 17, ProductName = "Alice Mutton", Category = "Meat/Poultry", UnitPrice = 39.0000M, UnitsInStock = 0 },
     81                     new Product { ProductID = 18, ProductName = "Carnarvon Tigers", Category = "Seafood", UnitPrice = 62.5000M, UnitsInStock = 42 },
     82                     new Product { ProductID = 19, ProductName = "Teatime Chocolate Biscuits", Category = "Confections", UnitPrice = 9.2000M, UnitsInStock = 25 },
     83                     new Product { ProductID = 20, ProductName = "Sir Rodney's Marmalade", Category = "Confections", UnitPrice = 81.0000M, UnitsInStock = 40 },
     84                     new Product { ProductID = 21, ProductName = "Sir Rodney's Scones", Category = "Confections", UnitPrice = 10.0000M, UnitsInStock = 3 },
     85                     new Product { ProductID = 22, ProductName = "Gustaf's Knäckebröd", Category = "Grains/Cereals", UnitPrice = 21.0000M, UnitsInStock = 104 },
     86                     new Product { ProductID = 23, ProductName = "Tunnbröd", Category = "Grains/Cereals", UnitPrice = 9.0000M, UnitsInStock = 61 },
     87                     new Product { ProductID = 24, ProductName = "Guaraná Fantástica", Category = "Beverages", UnitPrice = 4.5000M, UnitsInStock = 20 },
     88                     new Product { ProductID = 25, ProductName = "NuNuCa Nuß-Nougat-Creme", Category = "Confections", UnitPrice = 14.0000M, UnitsInStock = 76 },
     89                     new Product { ProductID = 26, ProductName = "Gumbär Gummibärchen", Category = "Confections", UnitPrice = 31.2300M, UnitsInStock = 15 },
     90                     new Product { ProductID = 27, ProductName = "Schoggi Schokolade", Category = "Confections", UnitPrice = 43.9000M, UnitsInStock = 49 },
     91                     new Product { ProductID = 28, ProductName = "Rössle Sauerkraut", Category = "Produce", UnitPrice = 45.6000M, UnitsInStock = 26 },
     92                     new Product { ProductID = 29, ProductName = "Thüringer Rostbratwurst", Category = "Meat/Poultry", UnitPrice = 123.7900M, UnitsInStock = 0 },
     93                     new Product { ProductID = 30, ProductName = "Nord-Ost Matjeshering", Category = "Seafood", UnitPrice = 25.8900M, UnitsInStock = 10 },
     94                     new Product { ProductID = 31, ProductName = "Gorgonzola Telino", Category = "Dairy Products", UnitPrice = 12.5000M, UnitsInStock = 0 },
     95                     new Product { ProductID = 32, ProductName = "Mascarpone Fabioli", Category = "Dairy Products", UnitPrice = 32.0000M, UnitsInStock = 9 },
     96                     new Product { ProductID = 33, ProductName = "Geitost", Category = "Dairy Products", UnitPrice = 2.5000M, UnitsInStock = 112 },
     97                     new Product { ProductID = 34, ProductName = "Sasquatch Ale", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 111 },
     98                     new Product { ProductID = 35, ProductName = "Steeleye Stout", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 20 },
     99                     new Product { ProductID = 36, ProductName = "Inlagd Sill", Category = "Seafood", UnitPrice = 19.0000M, UnitsInStock = 112 },
    100                     new Product { ProductID = 37, ProductName = "Gravad lax", Category = "Seafood", UnitPrice = 26.0000M, UnitsInStock = 11 },
    101                     new Product { ProductID = 38, ProductName = "Côte de Blaye", Category = "Beverages", UnitPrice = 263.5000M, UnitsInStock = 17 },
    102                     new Product { ProductID = 39, ProductName = "Chartreuse verte", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 69 },
    103                     new Product { ProductID = 40, ProductName = "Boston Crab Meat", Category = "Seafood", UnitPrice = 18.4000M, UnitsInStock = 123 },
    104                     new Product { ProductID = 41, ProductName = "Jack's New England Clam Chowder", Category = "Seafood", UnitPrice = 9.6500M, UnitsInStock = 85 },
    105                     new Product { ProductID = 42, ProductName = "Singaporean Hokkien Fried Mee", Category = "Grains/Cereals", UnitPrice = 14.0000M, UnitsInStock = 26 },
    106                     new Product { ProductID = 43, ProductName = "Ipoh Coffee", Category = "Beverages", UnitPrice = 46.0000M, UnitsInStock = 17 },
    107                     new Product { ProductID = 44, ProductName = "Gula Malacca", Category = "Condiments", UnitPrice = 19.4500M, UnitsInStock = 27 },
    108                     new Product { ProductID = 45, ProductName = "Rogede sild", Category = "Seafood", UnitPrice = 9.5000M, UnitsInStock = 5 },
    109                     new Product { ProductID = 46, ProductName = "Spegesild", Category = "Seafood", UnitPrice = 12.0000M, UnitsInStock = 95 },
    110                     new Product { ProductID = 47, ProductName = "Zaanse koeken", Category = "Confections", UnitPrice = 9.5000M, UnitsInStock = 36 },
    111                     new Product { ProductID = 48, ProductName = "Chocolade", Category = "Confections", UnitPrice = 12.7500M, UnitsInStock = 15 },
    112                     new Product { ProductID = 49, ProductName = "Maxilaku", Category = "Confections", UnitPrice = 20.0000M, UnitsInStock = 10 },
    113                     new Product { ProductID = 50, ProductName = "Valkoinen suklaa", Category = "Confections", UnitPrice = 16.2500M, UnitsInStock = 65 },
    114                     new Product { ProductID = 51, ProductName = "Manjimup Dried Apples", Category = "Produce", UnitPrice = 53.0000M, UnitsInStock = 20 },
    115                     new Product { ProductID = 52, ProductName = "Filo Mix", Category = "Grains/Cereals", UnitPrice = 7.0000M, UnitsInStock = 38 },
    116                     new Product { ProductID = 53, ProductName = "Perth Pasties", Category = "Meat/Poultry", UnitPrice = 32.8000M, UnitsInStock = 0 },
    117                     new Product { ProductID = 54, ProductName = "Tourtière", Category = "Meat/Poultry", UnitPrice = 7.4500M, UnitsInStock = 21 },
    118                     new Product { ProductID = 55, ProductName = "Pâté chinois", Category = "Meat/Poultry", UnitPrice = 24.0000M, UnitsInStock = 115 },
    119                     new Product { ProductID = 56, ProductName = "Gnocchi di nonna Alice", Category = "Grains/Cereals", UnitPrice = 38.0000M, UnitsInStock = 21 },
    120                     new Product { ProductID = 57, ProductName = "Ravioli Angelo", Category = "Grains/Cereals", UnitPrice = 19.5000M, UnitsInStock = 36 },
    121                     new Product { ProductID = 58, ProductName = "Escargots de Bourgogne", Category = "Seafood", UnitPrice = 13.2500M, UnitsInStock = 62 },
    122                     new Product { ProductID = 59, ProductName = "Raclette Courdavault", Category = "Dairy Products", UnitPrice = 55.0000M, UnitsInStock = 79 },
    123                     new Product { ProductID = 60, ProductName = "Camembert Pierrot", Category = "Dairy Products", UnitPrice = 34.0000M, UnitsInStock = 19 },
    124                     new Product { ProductID = 61, ProductName = "Sirop d'érable", Category = "Condiments", UnitPrice = 28.5000M, UnitsInStock = 113 },
    125                     new Product { ProductID = 62, ProductName = "Tarte au sucre", Category = "Confections", UnitPrice = 49.3000M, UnitsInStock = 17 },
    126                     new Product { ProductID = 63, ProductName = "Vegie-spread", Category = "Condiments", UnitPrice = 43.9000M, UnitsInStock = 24 },
    127                     new Product { ProductID = 64, ProductName = "Wimmers gute Semmelknödel", Category = "Grains/Cereals", UnitPrice = 33.2500M, UnitsInStock = 22 },
    128                     new Product { ProductID = 65, ProductName = "Louisiana Fiery Hot Pepper Sauce", Category = "Condiments", UnitPrice = 21.0500M, UnitsInStock = 76 },
    129                     new Product { ProductID = 66, ProductName = "Louisiana Hot Spiced Okra", Category = "Condiments", UnitPrice = 17.0000M, UnitsInStock = 4 },
    130                     new Product { ProductID = 67, ProductName = "Laughing Lumberjack Lager", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 52 },
    131                     new Product { ProductID = 68, ProductName = "Scottish Longbreads", Category = "Confections", UnitPrice = 12.5000M, UnitsInStock = 6 },
    132                     new Product { ProductID = 69, ProductName = "Gudbrandsdalsost", Category = "Dairy Products", UnitPrice = 36.0000M, UnitsInStock = 26 },
    133                     new Product { ProductID = 70, ProductName = "Outback Lager", Category = "Beverages", UnitPrice = 15.0000M, UnitsInStock = 15 },
    134                     new Product { ProductID = 71, ProductName = "Flotemysost", Category = "Dairy Products", UnitPrice = 21.5000M, UnitsInStock = 26 },
    135                     new Product { ProductID = 72, ProductName = "Mozzarella di Giovanni", Category = "Dairy Products", UnitPrice = 34.8000M, UnitsInStock = 14 },
    136                     new Product { ProductID = 73, ProductName = "Röd Kaviar", Category = "Seafood", UnitPrice = 15.0000M, UnitsInStock = 101 },
    137                     new Product { ProductID = 74, ProductName = "Longlife Tofu", Category = "Produce", UnitPrice = 10.0000M, UnitsInStock = 4 },
    138                     new Product { ProductID = 75, ProductName = "Rhönbräu Klosterbier", Category = "Beverages", UnitPrice = 7.7500M, UnitsInStock = 125 },
    139                     new Product { ProductID = 76, ProductName = "Lakkalikööri", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 57 },
    140                     new Product { ProductID = 77, ProductName = "Original Frankfurter grüne Soße", Category = "Condiments", UnitPrice = 13.0000M, UnitsInStock = 32 }
    141                 };
    142 
    143             // Customer/Order data read into memory from XML file using XLinq:
    144             customerList = (
    145                 from e in XDocument.Load("Customers.xml").
    146                           Root.Elements("customer")
    147                 select new Customer
    148                 {
    149                     CustomerID = (string)e.Element("id"),
    150                     CompanyName = (string)e.Element("name"),
    151                     Address = (string)e.Element("address"),
    152                     City = (string)e.Element("city"),
    153                     Region = (string)e.Element("region"),
    154                     PostalCode = (string)e.Element("postalcode"),
    155                     Country = (string)e.Element("country"),
    156                     Phone = (string)e.Element("phone"),
    157                     Fax = (string)e.Element("fax"),
    158                     Orders = (
    159                         from o in e.Elements("orders").Elements("order")
    160                         select new Order
    161                         {
    162                             OrderID = (int)o.Element("id"),
    163                             OrderDate = (DateTime)o.Element("orderdate"),
    164                             Total = (decimal)o.Element("total")
    165                         })
    166                         .ToArray()
    167                 })
    168                 .ToList();
    169         }
    170     }
    171 }

    Customers.xml (下载)

     1 <customer>
     2     <id>ALFKI</id>
     3     <name>Alfreds Futterkiste</name>
     4     <address>Obere Str. 57</address>
     5     <city>Berlin</city>
     6     <postalcode>12209</postalcode>
     7     <country>Germany</country>
     8     <phone>030-0074321</phone>
     9     <fax>030-0076545</fax>
    10     <orders>
    11       <order>
    12         <id>10643</id>
    13         <orderdate>1997-08-25T00:00:00</orderdate>
    14         <total>814.50</total>
    15       </order>
    16       <order>
    17         <id>10692</id>
    18         <orderdate>1997-10-03T00:00:00</orderdate>
    19         <total>878.00</total>
    20       </order>
    21       <order>
    22         <id>10702</id>
    23         <orderdate>1997-10-13T00:00:00</orderdate>
    24         <total>330.00</total>
    25       </order>
    26       <order>
    27         <id>10835</id>
    28         <orderdate>1998-01-15T00:00:00</orderdate>
    29         <total>845.80</total>
    30       </order>
    31       <order>
    32         <id>10952</id>
    33         <orderdate>1998-03-16T00:00:00</orderdate>
    34         <total>471.20</total>
    35       </order>
    36       <order>
    37         <id>11011</id>
    38         <orderdate>1998-04-09T00:00:00</orderdate>
    39         <total>933.50</total>
    40       </order>
    41    </orders>
    42 </customer>
    43     ...................
    44     ...................
    45     ...................
    46 <customer>
    47     <id>WOLZA</id>
    48     <name>Wolski  Zajazd</name>
    49     <address>ul. Filtrowa 68</address>
    50     <city>Warszawa</city>
    51     <postalcode>01-012</postalcode>
    52     <country>Poland</country>
    53     <phone>(26) 642-7012</phone>
    54     <fax>(26) 642-7012</fax>
    55     <orders>
    56       <order>
    57         <id>10374</id>
    58         <orderdate>1996-12-05T00:00:00</orderdate>
    59         <total>459.00</total>
    60       </order>
    61       <order>
    62         <id>10611</id>
    63         <orderdate>1997-07-25T00:00:00</orderdate>
    64         <total>808.00</total>
    65       </order>
    66       <order>
    67         <id>10792</id>
    68         <orderdate>1997-12-23T00:00:00</orderdate>
    69         <total>399.85</total>
    70       </order>
    71       <order>
    72         <id>10870</id>
    73         <orderdate>1998-02-04T00:00:00</orderdate>
    74         <total>160.00</total>
    75       </order>
    76       <order>
    77         <id>10906</id>
    78         <orderdate>1998-02-25T00:00:00</orderdate>
    79         <total>427.50</total>
    80       </order>
    81       <order>
    82         <id>10998</id>
    83         <orderdate>1998-04-03T00:00:00</orderdate>
    84         <total>686.00</total>
    85       </order>
    86       <order>
    87         <id>11044</id>
    88         <orderdate>1998-04-23T00:00:00</orderdate>
    89         <total>591.60</total>
    90       </order>
    91    </orders>
    92 </customer>
    • ObjectDumper.cs
      1 //Copyright (C) Microsoft Corporation.  All rights reserved. 
      2 
      3 using System;
      4 using System.Collections;
      5 using System.IO;
      6 using System.Reflection;
      7 
      8 // See the ReadMe.html for additional information 
      9 namespace Linq101
     10 {
     11     public class ObjectDumper
     12     {
     13 
     14         public static void Write(object element)
     15         {
     16             Write(element, 0);
     17         }
     18 
     19         public static void Write(object element, int depth)
     20         {
     21             Write(element, depth, Console.Out);
     22         }
     23 
     24         public static void Write(object element, int depth, TextWriter log)
     25         {
     26             ObjectDumper dumper = new ObjectDumper(depth) {writer = log};
     27             dumper.WriteObject(null, element);
     28         }
     29 
     30         TextWriter writer;
     31         int pos;
     32         int level;
     33         int depth;
     34 
     35         private ObjectDumper(int depth)
     36         {
     37             this.depth = depth;
     38         }
     39 
     40         private void Write(string s)
     41         {
     42             if (s != null)
     43             {
     44                 writer.Write(s);
     45                 pos += s.Length;
     46             }
     47         }
     48 
     49         private void WriteIndent()
     50         {
     51             for (int i = 0; i < level; i++) writer.Write("  ");
     52         }
     53 
     54         private void WriteLine()
     55         {
     56             writer.WriteLine();
     57             pos = 0;
     58         }
     59 
     60         private void WriteTab()
     61         {
     62             Write("  ");
     63             while (pos % 8 != 0) Write(" ");
     64         }
     65 
     66         private void WriteObject(string prefix, object element)
     67         {
     68             if (element == null || element is ValueType || element is string)
     69             {
     70                 WriteIndent();
     71                 Write(prefix);
     72                 WriteValue(element);
     73                 WriteLine();
     74             }
     75             else
     76             {
     77                 IEnumerable enumerableElement = element as IEnumerable;
     78                 if (enumerableElement != null)
     79                 {
     80                     foreach (object item in enumerableElement)
     81                     {
     82                         if (item is IEnumerable && !(item is string))
     83                         {
     84                             WriteIndent();
     85                             Write(prefix);
     86                             Write("...");
     87                             WriteLine();
     88                             if (level < depth)
     89                             {
     90                                 level++;
     91                                 WriteObject(prefix, item);
     92                                 level--;
     93                             }
     94                         }
     95                         else
     96                         {
     97                             WriteObject(prefix, item);
     98                         }
     99                     }
    100                 }
    101                 else
    102                 {
    103                     MemberInfo[] members = element.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance);
    104                     WriteIndent();
    105                     Write(prefix);
    106                     bool propWritten = false;
    107                     foreach (MemberInfo m in members)
    108                     {
    109                         FieldInfo f = m as FieldInfo;
    110                         PropertyInfo p = m as PropertyInfo;
    111                         if (f != null || p != null)
    112                         {
    113                             if (propWritten)
    114                             {
    115                                 WriteTab();
    116                             }
    117                             else
    118                             {
    119                                 propWritten = true;
    120                             }
    121                             Write(m.Name);
    122                             Write("=");
    123                             Type t = f != null ? f.FieldType : p.PropertyType;
    124                             if (t.IsValueType || t == typeof(string))
    125                             {
    126                                 WriteValue(f != null ? f.GetValue(element) : p.GetValue(element, null));
    127                             }
    128                             else
    129                             {
    130                                 if (typeof(IEnumerable).IsAssignableFrom(t))
    131                                 {
    132                                     Write("...");
    133                                 }
    134                                 else
    135                                 {
    136                                     Write("{ }");
    137                                 }
    138                             }
    139                         }
    140                     }
    141                     if (propWritten) WriteLine();
    142                     if (level < depth)
    143                     {
    144                         foreach (MemberInfo m in members)
    145                         {
    146                             FieldInfo f = m as FieldInfo;
    147                             PropertyInfo p = m as PropertyInfo;
    148                             if (f != null || p != null)
    149                             {
    150                                 Type t = f != null ? f.FieldType : p.PropertyType;
    151                                 if (!(t.IsValueType || t == typeof(string)))
    152                                 {
    153                                     object value = f != null ? f.GetValue(element) : p.GetValue(element, null);
    154                                     if (value != null)
    155                                     {
    156                                         level++;
    157                                         WriteObject(m.Name + ": ", value);
    158                                         level--;
    159                                     }
    160                                 }
    161                             }
    162                         }
    163                     }
    164                 }
    165             }
    166         }
    167 
    168         private void WriteValue(object o)
    169         {
    170             if (o == null)
    171             {
    172                 Write("null");
    173             }
    174             else if (o is DateTime)
    175             {
    176                 Write(((DateTime)o).ToShortDateString());
    177             }
    178             else if (o is ValueType || o is string)
    179             {
    180                 Write(o.ToString());
    181             }
    182             else if (o is IEnumerable)
    183             {
    184                 Write("...");
    185             }
    186             else
    187             {
    188                 Write("{ }");
    189             }
    190         }
    191     }
    192 }
  • 相关阅读:
    datagridview 查询数据库数据
    java 类属性的加载顺序(带有继承关系的)
    jquery控制checkbox
    dataTable获取全部输入的数据(不仅是页面中展示出来的)
    java.security.Key 在main方法中与在tomcat中得到的encode不同,why?
    java代理模式
    android webservice交互开发
    完成oracle数据分页功能
    android 接收服务端 oracle数据中存储的blob对象 转化为图像
    JSON使用详解
  • 原文地址:https://www.cnblogs.com/David-Huang/p/4126629.html
Copyright © 2020-2023  润新知