原文地址: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. xamarin SimpleAdapter绑定出错问题

    问题:今天在实验xamarin中SimpleAdapter绑定到ListView时,出现闪退的现象, 见图: 解决方法: SimpleAdapter中的构造函数public SimpleAdapter ...

  2. Xcode 5 解决 The operation couldn’t be completed. (NSURLErrorDomain error -1012.) 问题

    使用Xcode6.1 SVN 出现问题 The operation couldn’t be completed. (NSURLErrorDomain error -1012.) 解决方法: 打开终端 ...

  3. Js使用word书签填充内容

    Js使用word书签填充内容 1.在模板文件中需要填充的地方插入书签 填充内容为:(|光标所在处) 填写书签名,点击添加完成: 2.使用js打开模板,获取书签位置,填充数据: function pri ...

  4. Yaroslav and Sequence

    Codeforces Round #182 (Div. 1) A:http://codeforces.com/contest/301/problem/A 题意:给你2*n-1个数,你每次可以选择n个连 ...

  5. Atlantis

    poj1151:http://poj.org/problem?id=1151 题意:求矩形面积的并题解:扫描线加线段树 #include<iostream> #include<cst ...

  6. iOS cell自动换行

    // //  DynamicHeightsViewController.h //  DynamicHeights // //  Created by Matt Long on 9/22/09. //  ...

  7. Buffer Sort

    BUFFER (SORT) Description Performs a memory sort on a row source CREATE TABLE t1 (c01 NUMBER); CREAT ...

  8. 【Linux】鸟哥的Linux私房菜基础学习篇整理(八)

    1. useradd [-u UID] [-g 初始用户组] [-G 次要用户组] [-mM]\   [-c 说明栏] [-d 主文件夹绝对路径] [-r] [-s shell] 用户账号名:新增用户 ...

  9. 【算法Everyday】第三日 KMP算法

    题目 你知道的. 分析 分析不来. 代码 void OutputArray(int* pArr, int iLen) { ; i < iLen; i++) { printf("%d\t ...

  10. 【动态规划】【二分】【最长上升子序列】Vijos P1028 魔族密码

    题目链接: https://vijos.org/p/1028 题目大意: 给N个字符串(N<=2000),求能组成词链的单词最多有几个. 如果在一个由一个词或多个词组成的表中,除了最后一个以外, ...