Linq编程101例

时间:2023-03-09 12:55:27
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
 using System;

 namespace Linq101
{
public class Program
{
private static void Main()
{
#region Restriction
//Restriction restriction = new Restriction();
//restriction.Simple1();//Where - Simple 1
//restriction.Simple2();//Where - Simple 2
//restriction.Simple3();//Where - Simple 3
//restriction.Simple4();//Where - Drilldown
//restriction.Simple5();//Where - Indexed
#endregion #region Projection
//Projection projection = new Projection();
//projection.Linq6();//Select - Simple 1
//projection.Linq7();//Select - Simple 2
//projection.Linq8();//Select - Transformation
//projection.Linq9();//Select - Anonymous Types 1
//projection.Linq10();//Select - Anonymous Types 2
//projection.Linq11();//Select - Anonymous Types 3
//projection.Linq12();//Select - Indexed
//projection.Linq13();//Select - Filtered
//projection.Linq14();//SelectMany - Compound from 1
//projection.Linq15();//SelectMany - Compound from 2
//projection.Linq16();//SelectMany - Compound from 3
//projection.Linq17();//SelectMany - from Assignment
//projection.Linq18();//SelectMany - Multiple from
//projection.Linq19();//SelectMany - Indexed
#endregion #region Partitioning
//Partitioning partitioning = new Partitioning();
//partitioning.Linq20();//Take - Simple
//partitioning.Linq21();//Take - Nested
//partitioning.Linq22();//Skip - Simple
//partitioning.Linq23();//Skip - Nested
//partitioning.Linq24();//TakeWhile - Simple
//partitioning.Linq25();//TakeWhile - Indexed
//partitioning.Linq26();//SkipWhile - Simple
//partitioning.Linq27();//SkipWhile - Indexed
#endregion #region Ordering
//Ordering ordering = new Ordering();
//ordering.Linq28();//OrderBy - Simple 1
//ordering.Linq29();//OrderBy - Simple 2
//ordering.Linq30();//OrderBy - Simple 3
//ordering.Linq31();//OrderBy - Comparer
//ordering.Linq32();//OrderByDescending - Simple 1
//ordering.Linq33();//OrderByDescending - Simple 2
//ordering.Linq34();//OrderByDescending - Comparer
//ordering.Linq35();//ThenBy - Simple
//ordering.Linq36();//ThenBy - Comparer
//ordering.Linq37();//ThenByDescending - Simple
//ordering.Linq38();//ThenByDescending - Comparer
//ordering.Linq39();//Reverse
#endregion #region Grouping
//Grouping grouping = new Grouping();
//grouping.Linq40();//GroupBy - Simple 1
//grouping.Linq41();//GroupBy - Simple 2
//grouping.Linq42();//GroupBy - Simple 3
//grouping.Linq43();//GroupBy - Nested
//grouping.Linq44();//GroupBy - Comparer
//grouping.Linq45();//GroupBy - Comparer, Mapped
#endregion #region Set
//Set set = new Set();
//set.Linq46();//Distinct - 1
//set.Linq47();//Distinct - 2
//set.Linq48();//Union - 1
//set.Linq49();//Union - 2
//set.Linq50();//Intersect - 1
//set.Linq51();//Intersect - 2
//set.Linq52();//Except - 1
//set.Linq53();//Except - 2
#endregion #region Conversion
//Conversion conversion=new Conversion();
//conversion.Linq54();//ToArray
//conversion.Linq55();//ToList
//conversion.Linq56();//ToDictionary
//conversion.Linq57();//OfType
#endregion #region Element
//Element element=new Element();
//element.Linq58();//First - Simple
//element.Linq59();//First - Condition
//element.Linq60();//FirstOrDefault - Simple
//element.Linq61();//FirstOrDefault - Condition
//element.Linq62();//ElementAt
#endregion #region Generation
//Generation generation = new Generation();
//generation.Linq65();//Range
//generation.Linq66();//Repeat
#endregion #region Quantifiers
//Quantifiers quantifiers=new Quantifiers();
//quantifiers.Linq67();//Any - Simple
//quantifiers.Linq69();//All - Grouped
//quantifiers.Linq70();//All - Simple
//quantifiers.Linq72();//All - Grouped
#endregion #region Aggregate
//Aggregate aggregate = new Aggregate();
//aggregate.Linq73();//Count - Simple
//aggregate.Linq74();//Count - Conditional
//aggregate.Linq76();//Count - Nested
//aggregate.Linq77();//Count - Grouped
//aggregate.Linq78();//Sum - Simple
//aggregate.Linq79();//Sum - Projection
//aggregate.Linq80();//Sum - Grouped
//aggregate.Linq81();//Min - Simple
//aggregate.Linq82();//Min - Projection
//aggregate.Linq83();//Min - Grouped
//aggregate.Linq84();//Min - Elements
//aggregate.Linq85();//Max - Simple
//aggregate.Linq86();//Max - Projection
//aggregate.Linq87();//Max - Grouped
//aggregate.Linq88();//Max - Elements
//aggregate.Linq89();//Average - Simple
//aggregate.Linq90();//Average - Projection
//aggregate.Linq91();//Average - Grouped
//aggregate.Linq92();//Aggregate - Simple
//aggregate.Linq93();//Aggregate - Seed
#endregion #region Miscellaneous
//Miscellaneous miscellaneous=new Miscellaneous();
//miscellaneous.Linq94();//Concat - 1
//miscellaneous.Linq95();//Concat - 2
//miscellaneous.Linq96();//EqualAll - 1
//miscellaneous.Linq97();//EqualAll - 2
#endregion #region CustomSequence
//CustomSequence customSequence =new CustomSequence();
//customSequence.Linq98();//Combine
#endregion #region QueryExecution
//QueryExecution queryExecution = new QueryExecution();
//queryExecution.Linq99();//Deferred Execution
//queryExecution.Linq100();//Immediate Execution
//queryExecution.Linq101();//Query Reuse
#endregion #region Join
Join join=new Join();
//join.Linq102();//Cross Join
//join.Linq103();//Group Join
//join.Linq104();//Cross Join with Group Join
//join.Linq105();//Left Outer Join
#endregion
Console.ReadLine();
}
}
}
  • Data.cs
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq; namespace Linq101
{
class Data
{
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public string Category { get; set; }
public decimal UnitPrice { get; set; }
public int UnitsInStock { get; set; }
} public class Order
{
public int OrderID { get; set; }
public DateTime OrderDate { get; set; }
public decimal Total { get; set; }
} public class Customer
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public Order[] Orders { get; set; }
} private static List<Product> productList;
private static List<Customer> customerList; public static List<Product> GetProductList()
{
if (productList == null)
CreateLists(); return productList;
} public static List<Customer> GetCustomerList()
{
if (customerList == null)
CreateLists(); return customerList;
} private static void CreateLists()
{
// Product data created in-memory using collection initializer:
productList =
new List<Product> {
new Product { ProductID = , ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Aniseed Syrup", Category = "Condiments", UnitPrice = 10.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Chef Anton's Cajun Seasoning", Category = "Condiments", UnitPrice = 22.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Chef Anton's Gumbo Mix", Category = "Condiments", UnitPrice = 21.3500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Grandma's Boysenberry Spread", Category = "Condiments", UnitPrice = 25.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Uncle Bob's Organic Dried Pears", Category = "Produce", UnitPrice = 30.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Northwoods Cranberry Sauce", Category = "Condiments", UnitPrice = 40.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Mishi Kobe Niku", Category = "Meat/Poultry", UnitPrice = 97.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Ikura", Category = "Seafood", UnitPrice = 31.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Queso Cabrales", Category = "Dairy Products", UnitPrice = 21.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Queso Manchego La Pastora", Category = "Dairy Products", UnitPrice = 38.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Konbu", Category = "Seafood", UnitPrice = 6.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Tofu", Category = "Produce", UnitPrice = 23.2500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Genen Shouyu", Category = "Condiments", UnitPrice = 15.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Pavlova", Category = "Confections", UnitPrice = 17.4500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Alice Mutton", Category = "Meat/Poultry", UnitPrice = 39.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Carnarvon Tigers", Category = "Seafood", UnitPrice = 62.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Teatime Chocolate Biscuits", Category = "Confections", UnitPrice = 9.2000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Sir Rodney's Marmalade", Category = "Confections", UnitPrice = 81.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Sir Rodney's Scones", Category = "Confections", UnitPrice = 10.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Gustaf's Knäckebröd", Category = "Grains/Cereals", UnitPrice = 21.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Tunnbröd", Category = "Grains/Cereals", UnitPrice = 9.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Guaraná Fantástica", Category = "Beverages", UnitPrice = 4.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "NuNuCa Nuß-Nougat-Creme", Category = "Confections", UnitPrice = 14.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Gumbär Gummibärchen", Category = "Confections", UnitPrice = 31.2300M, UnitsInStock = },
new Product { ProductID = , ProductName = "Schoggi Schokolade", Category = "Confections", UnitPrice = 43.9000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Rössle Sauerkraut", Category = "Produce", UnitPrice = 45.6000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Thüringer Rostbratwurst", Category = "Meat/Poultry", UnitPrice = 123.7900M, UnitsInStock = },
new Product { ProductID = , ProductName = "Nord-Ost Matjeshering", Category = "Seafood", UnitPrice = 25.8900M, UnitsInStock = },
new Product { ProductID = , ProductName = "Gorgonzola Telino", Category = "Dairy Products", UnitPrice = 12.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Mascarpone Fabioli", Category = "Dairy Products", UnitPrice = 32.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Geitost", Category = "Dairy Products", UnitPrice = 2.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Sasquatch Ale", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Steeleye Stout", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Inlagd Sill", Category = "Seafood", UnitPrice = 19.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Gravad lax", Category = "Seafood", UnitPrice = 26.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Côte de Blaye", Category = "Beverages", UnitPrice = 263.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Chartreuse verte", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Boston Crab Meat", Category = "Seafood", UnitPrice = 18.4000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Jack's New England Clam Chowder", Category = "Seafood", UnitPrice = 9.6500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Singaporean Hokkien Fried Mee", Category = "Grains/Cereals", UnitPrice = 14.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Ipoh Coffee", Category = "Beverages", UnitPrice = 46.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Gula Malacca", Category = "Condiments", UnitPrice = 19.4500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Rogede sild", Category = "Seafood", UnitPrice = 9.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Spegesild", Category = "Seafood", UnitPrice = 12.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Zaanse koeken", Category = "Confections", UnitPrice = 9.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Chocolade", Category = "Confections", UnitPrice = 12.7500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Maxilaku", Category = "Confections", UnitPrice = 20.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Valkoinen suklaa", Category = "Confections", UnitPrice = 16.2500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Manjimup Dried Apples", Category = "Produce", UnitPrice = 53.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Filo Mix", Category = "Grains/Cereals", UnitPrice = 7.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Perth Pasties", Category = "Meat/Poultry", UnitPrice = 32.8000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Tourtière", Category = "Meat/Poultry", UnitPrice = 7.4500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Pâté chinois", Category = "Meat/Poultry", UnitPrice = 24.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Gnocchi di nonna Alice", Category = "Grains/Cereals", UnitPrice = 38.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Ravioli Angelo", Category = "Grains/Cereals", UnitPrice = 19.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Escargots de Bourgogne", Category = "Seafood", UnitPrice = 13.2500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Raclette Courdavault", Category = "Dairy Products", UnitPrice = 55.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Camembert Pierrot", Category = "Dairy Products", UnitPrice = 34.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Sirop d'érable", Category = "Condiments", UnitPrice = 28.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Tarte au sucre", Category = "Confections", UnitPrice = 49.3000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Vegie-spread", Category = "Condiments", UnitPrice = 43.9000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Wimmers gute Semmelknödel", Category = "Grains/Cereals", UnitPrice = 33.2500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Louisiana Fiery Hot Pepper Sauce", Category = "Condiments", UnitPrice = 21.0500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Louisiana Hot Spiced Okra", Category = "Condiments", UnitPrice = 17.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Laughing Lumberjack Lager", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Scottish Longbreads", Category = "Confections", UnitPrice = 12.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Gudbrandsdalsost", Category = "Dairy Products", UnitPrice = 36.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Outback Lager", Category = "Beverages", UnitPrice = 15.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Flotemysost", Category = "Dairy Products", UnitPrice = 21.5000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Mozzarella di Giovanni", Category = "Dairy Products", UnitPrice = 34.8000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Röd Kaviar", Category = "Seafood", UnitPrice = 15.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Longlife Tofu", Category = "Produce", UnitPrice = 10.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Rhönbräu Klosterbier", Category = "Beverages", UnitPrice = 7.7500M, UnitsInStock = },
new Product { ProductID = , ProductName = "Lakkalikööri", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = },
new Product { ProductID = , ProductName = "Original Frankfurter grüne Soße", Category = "Condiments", UnitPrice = 13.0000M, UnitsInStock = }
}; // Customer/Order data read into memory from XML file using XLinq:
customerList = (
from e in XDocument.Load("Customers.xml").
Root.Elements("customer")
select new Customer
{
CustomerID = (string)e.Element("id"),
CompanyName = (string)e.Element("name"),
Address = (string)e.Element("address"),
City = (string)e.Element("city"),
Region = (string)e.Element("region"),
PostalCode = (string)e.Element("postalcode"),
Country = (string)e.Element("country"),
Phone = (string)e.Element("phone"),
Fax = (string)e.Element("fax"),
Orders = (
from o in e.Elements("orders").Elements("order")
select new Order
{
OrderID = (int)o.Element("id"),
OrderDate = (DateTime)o.Element("orderdate"),
Total = (decimal)o.Element("total")
})
.ToArray()
})
.ToList();
}
}
}

Customers.xml (下载)

 <customer>
<id>ALFKI</id>
<name>Alfreds Futterkiste</name>
<address>Obere Str. 57</address>
<city>Berlin</city>
<postalcode>12209</postalcode>
<country>Germany</country>
<phone>030-0074321</phone>
<fax>030-0076545</fax>
<orders>
<order>
<id>10643</id>
<orderdate>1997-08-25T00:00:00</orderdate>
<total>814.50</total>
</order>
<order>
<id>10692</id>
<orderdate>1997-10-03T00:00:00</orderdate>
<total>878.00</total>
</order>
<order>
<id>10702</id>
<orderdate>1997-10-13T00:00:00</orderdate>
<total>330.00</total>
</order>
<order>
<id>10835</id>
<orderdate>1998-01-15T00:00:00</orderdate>
<total>845.80</total>
</order>
<order>
<id>10952</id>
<orderdate>1998-03-16T00:00:00</orderdate>
<total>471.20</total>
</order>
<order>
<id>11011</id>
<orderdate>1998-04-09T00:00:00</orderdate>
<total>933.50</total>
</order>
</orders>
</customer>
...................
...................
...................
<customer>
<id>WOLZA</id>
<name>Wolski Zajazd</name>
<address>ul. Filtrowa 68</address>
<city>Warszawa</city>
<postalcode>01-012</postalcode>
<country>Poland</country>
<phone>(26) 642-7012</phone>
<fax>(26) 642-7012</fax>
<orders>
<order>
<id>10374</id>
<orderdate>1996-12-05T00:00:00</orderdate>
<total>459.00</total>
</order>
<order>
<id>10611</id>
<orderdate>1997-07-25T00:00:00</orderdate>
<total>808.00</total>
</order>
<order>
<id>10792</id>
<orderdate>1997-12-23T00:00:00</orderdate>
<total>399.85</total>
</order>
<order>
<id>10870</id>
<orderdate>1998-02-04T00:00:00</orderdate>
<total>160.00</total>
</order>
<order>
<id>10906</id>
<orderdate>1998-02-25T00:00:00</orderdate>
<total>427.50</total>
</order>
<order>
<id>10998</id>
<orderdate>1998-04-03T00:00:00</orderdate>
<total>686.00</total>
</order>
<order>
<id>11044</id>
<orderdate>1998-04-23T00:00:00</orderdate>
<total>591.60</total>
</order>
</orders>
</customer>
  • ObjectDumper.cs
 //Copyright (C) Microsoft Corporation.  All rights reserved. 

 using System;
using System.Collections;
using System.IO;
using System.Reflection; // See the ReadMe.html for additional information
namespace Linq101
{
public class ObjectDumper
{ public static void Write(object element)
{
Write(element, );
} public static void Write(object element, int depth)
{
Write(element, depth, Console.Out);
} public static void Write(object element, int depth, TextWriter log)
{
ObjectDumper dumper = new ObjectDumper(depth) {writer = log};
dumper.WriteObject(null, element);
} TextWriter writer;
int pos;
int level;
int depth; private ObjectDumper(int depth)
{
this.depth = depth;
} private void Write(string s)
{
if (s != null)
{
writer.Write(s);
pos += s.Length;
}
} private void WriteIndent()
{
for (int i = ; i < level; i++) writer.Write(" ");
} private void WriteLine()
{
writer.WriteLine();
pos = ;
} private void WriteTab()
{
Write(" ");
while (pos % != ) Write(" ");
} private void WriteObject(string prefix, object element)
{
if (element == null || element is ValueType || element is string)
{
WriteIndent();
Write(prefix);
WriteValue(element);
WriteLine();
}
else
{
IEnumerable enumerableElement = element as IEnumerable;
if (enumerableElement != null)
{
foreach (object item in enumerableElement)
{
if (item is IEnumerable && !(item is string))
{
WriteIndent();
Write(prefix);
Write("...");
WriteLine();
if (level < depth)
{
level++;
WriteObject(prefix, item);
level--;
}
}
else
{
WriteObject(prefix, item);
}
}
}
else
{
MemberInfo[] members = element.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance);
WriteIndent();
Write(prefix);
bool propWritten = false;
foreach (MemberInfo m in members)
{
FieldInfo f = m as FieldInfo;
PropertyInfo p = m as PropertyInfo;
if (f != null || p != null)
{
if (propWritten)
{
WriteTab();
}
else
{
propWritten = true;
}
Write(m.Name);
Write("=");
Type t = f != null ? f.FieldType : p.PropertyType;
if (t.IsValueType || t == typeof(string))
{
WriteValue(f != null ? f.GetValue(element) : p.GetValue(element, null));
}
else
{
if (typeof(IEnumerable).IsAssignableFrom(t))
{
Write("...");
}
else
{
Write("{ }");
}
}
}
}
if (propWritten) WriteLine();
if (level < depth)
{
foreach (MemberInfo m in members)
{
FieldInfo f = m as FieldInfo;
PropertyInfo p = m as PropertyInfo;
if (f != null || p != null)
{
Type t = f != null ? f.FieldType : p.PropertyType;
if (!(t.IsValueType || t == typeof(string)))
{
object value = f != null ? f.GetValue(element) : p.GetValue(element, null);
if (value != null)
{
level++;
WriteObject(m.Name + ": ", value);
level--;
}
}
}
}
}
}
}
} private void WriteValue(object o)
{
if (o == null)
{
Write("null");
}
else if (o is DateTime)
{
Write(((DateTime)o).ToShortDateString());
}
else if (o is ValueType || o is string)
{
Write(o.ToString());
}
else if (o is IEnumerable)
{
Write("...");
}
else
{
Write("{ }");
}
}
}
}