原文地址: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("{ }");
}
}
}
}

Linq编程101例的更多相关文章

  1. 【算法】C语言趣味程序设计编程百例精解

    C语言趣味程序设计编程百例精解 C/C++语言经典.实用.趣味程序设计编程百例精解(1)  https://wenku.baidu.com/view/b9f683c08bd63186bcebbc3c. ...

  2. linux下C语言socket网络编程简例

    原创文章,转载请注明转载字样和出处,谢谢! 这里给出在linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到client的连接后,发送数据给client:clie ...

  3. 5.3linux下C语言socket网络编程简例

    原创文章,转载请注明转载字样和出处,谢谢! 这里给出在Linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到客户端的连接后,发送数据给客户端:客户端在接受到数据后 ...

  4. C# LINQ Unity 单例

    C# LINQ   1. 自定义 Master,Kongfu 类 1 class Master 2 { 3 4 public int Id { get; set; } 5 public string ...

  5. javascript函数式编程一例分析

    js像其他动态语言一样是可以写高阶函数的,所谓高阶函数是可以操作函数的函数.因为在js中函数是一个彻彻底底的对象,属于第一类公民,这提供了函数式编程的先决条件. 下面给出一个例子代码,出自一本js教程 ...

  6. Linq编程小趣味爱因斯坦谜题

    最近看到一个比较老的题目,题目----在一条街上,有5座房子,喷了5种颜色,每个房里住着不同国籍的人,每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物,问题---谁养鱼? 以前没事还做过这个题,现在 ...

  7. 微软关于LINQ的101个例子

    记录,备查. 101 LINQ Sqmples

  8. 2) LINQ编程技术内幕--yield return

    yield return 使用.NET的状态机生成器 yield return关键词组自动实现IDisposable,使用这个可枚举的地方, 还存在一个隐含的try finally块. 示例代码: c ...

  9. 一,python编程100例

    1.有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? number = (1 ,2,3,4) ...

随机推荐

  1. iscc2016-basic-心灵鸡汤

    用winhex打开发现 ISCCCongratulations! You need remember: DEath IS JUST A PaRT oF lIFE,sOMeTHInG wE'RE aLL ...

  2. [BZOJ 1576] [Usaco2009 Jan] 安全路经Travel 【树链剖分】

    题目链接: BZOJ - 1576 题目分析 首先Orz Hzwer的题解. 先使用 dijikstra 求出最短路径树. 那么对于一条不在最短路径树上的边 (u -> v, w) 我们可以先沿 ...

  3. 45 Useful JavaScript Tips, Tricks and Best Practices(有用的JavaScript技巧,技巧和最佳实践)

    As you know, JavaScript is the number one programming language in the world, the language of the web ...

  4. Struts2 cookie的存取

    /** * Cookieの追加 * @return * @throws Exception */ private void addCookie(String name,String value){ C ...

  5. 3.Repeater 绑定数据例子

    此例子绑定的数据源为微软在mssql2000中提供的Northwind数据库中的表Categories. 以下为设计步骤: 在C# 中连接数据库.如下图: 在项目中添加新建项,建立一个数据集,并把Ca ...

  6. QT、QTE、qtopia区别

    QT.QTE.qtopia区别 Qt的授权是分为两条线,商业版和开源版.如果使用商业版的Qt,那么开发出的程序可以是私有的和商业的:如果使用的是开源版的Qt,由于其使用的是GPL协议,那么可发出的程序 ...

  7. 浅度围观SBJson

    JSON JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度, 那么,JSON到底是什么? JSON就是一串字符串 只不 ...

  8. 【转】google chrome如何设置主页

    原文网址:http://jingyan.baidu.com/article/8275fc86bf916c46a13cf666.html google chrome是一款拥有众多优秀插件的浏览器,是我们 ...

  9. 数据结构(KD树):HDU 4347 The Closest M Points

    The Closest M Points Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Ot ...

  10. Subsets II ——LeetCode

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...