using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestLambdaExpression2
{
static class Program
{
static void Main(string[] args)
{
List allPerson = GetAllPerson();
#region Func
//1.
Func FuncSelector = new Func(heightselector);
Console.WriteLine(GetMaxFunc(allPerson, FuncSelector));
//2.
Func FuncSelector2 = heightselector;
Console.WriteLine(GetMaxFunc(allPerson, FuncSelector2));
//3.
Console.WriteLine(GetMaxFunc(allPerson, heightselector));
//4.
Console.WriteLine(allPerson.GetMaxFunc(heightselector));
//5.使用Lambda Expressions精簡Func之寫作
Console.WriteLine(allPerson.GetMaxFunc(p => p.height));
//6.因為Max為多型,必須先宣告形式才不會發生錯誤
Func FuncSelector3 = heightselector;
Console.WriteLine(allPerson.Max(FuncSelector3));
#endregion
#region Delegate
//1.
heightestSelector DelegateSelector = heightselector;
Console.WriteLine(GetMaxDelegate(allPerson, DelegateSelector));
//2.
Console.WriteLine(GetMaxDelegate(allPerson, heightselector));
//3.
Console.WriteLine(allPerson.GetMaxDelegate(heightselector));
//4.AnonymousDelegate
Console.WriteLine(allPerson.GetMaxDelegate(delegate(Person p) { return p.height; }));
//5.使用Lambda Expressions精簡AnonymousDelegate之寫作
Console.WriteLine(allPerson.GetMaxDelegate(p => p.height));
//6.會發生錯誤,因System.linq.Max需定義Func..不可傳入Delegate,會無法判定型式
//heightestSelector DelegateSelector2 = heightselector;
//Console.WriteLine(allPerson.Max(DelegateSelector2));
//7.使用System.linq.Max,AnonymousDelegate
Console.WriteLine(allPerson.Max(delegate(Person p) { return p.height; }));
//8.使用System.linq.Max,Lambda Expressions精簡AnonymousDelegate之寫作
Console.WriteLine(allPerson.Max(p => p.height));
Man[] mans = Array.ConvertAll(allPerson.ToArray(), p => new Man() { name = p.name, weight = p.weight });
Console.WriteLine(mans.Max(p => p.weight));
#endregion
}
public delegate int heightestSelector(Person p);
public static int heightselector(Person p) { return p.height; }
private static int GetMaxFunc(this List allPerson, Func selector)
{
return (from i in allPerson
select selector(i)).Max();
}
//private static decimal GetMaxFunc(this List allPerson, Func selector)
//{
// return (from i in allPerson
// select selector(i)).Max();
//}
private static int GetMaxDelegate(this List allPerson, heightestSelector selector)
{
return (from i in allPerson
select selector(i)).Max();
}
static List GetAllPerson()
{
List persons = new List();
persons.Add(new Person { name = "A", height = 150, weight = 50 });
persons.Add(new Person { name = "B", height = 160, weight = 60 });
persons.Add(new Person { name = "C", height = 170, weight = 70 });
persons.Add(new Person { name = "D", height = 180, weight = 80 });
persons.Add(new Person { name = "E", height = 190, weight = 90 });
return persons;
}
}
public class Person
{
public string name { get; set; }
public int height { get; set; }
public int weight { get; set; }
}
public class Man
{
public string name { get; set; }
public int weight { get; set; }
}
}
網誌標籤
.NET Framework
(5)
文字編碼
(4)
防毒軟體
(1)
其他
(11)
資料庫
(25)
攝影
(2)
ADO.NET Entity Framework
(2)
ASP.NET
(49)
ASP.NET MVC
(4)
ASP.NET Test
(4)
CSS
(1)
Design Patten
(1)
FCKEditor
(3)
HTML
(2)
HTML5
(1)
hyper-v
(1)
IIS
(6)
JavaScript
(9)
JQuery
(1)
LINQ
(2)
MailTo編碼
(2)
oracle
(6)
RegularExpression
(1)
Rhino Mocks
(1)
Security
(2)
Server2008R2
(1)
SharePoint
(5)
Sql 2008
(1)
SSIS
(1)
SyntaxHighlighter
(1)
TFS2010
(1)
VirtualBox
(1)
VistualStudio2010
(3)
vs2003升級vs2008
(1)
windows
(1)
windows 7
(1)
Delegate,AnonymousDelegate,Func,LambdaExpression,Linq演化過程程式範例
LINQ強大威力
今天下午花了些時間研究 LINQ,這果然是殺手級的東西啊!
光是去 http://www.asp.net/learn/linq-videos/ 這裡看了四個影片,就覺得真是不可思議的強,LINQ to SQL 真是好東西,解決了許多之前 Typed DataSet 做不到的事,又將整個資料庫操作的過程徹底的物件化與簡化,跟 SQL Server 的整合又更完整了。
大家有興趣可以去看看影片,沒幾分鐘:
http://www.asp.net/learn/linq-videos/
底下是在 Scott Guthrie 網站的文章,寫得很清楚且圖文並茂,看完影片可以來這裡建立更完整的觀念:
Part 1: Introduction to LINQ to SQL
Part 2: Defining our Data Model Classes
Part 3: Querying our Database
Part 4: Updating our Database
Part 5: Binding UI using the ASP:LinqDataSource Control
Part 6: Retrieving Data Using Stored Procedures
Part 7: Updating our Database using Stored Procedures
Part 8: Executing Custom SQL Expressions
Part 9 - Using a Custom LINQ Expression with the control
訂閱:
意見 (Atom)
