Lambda开启新的编写方式
出于对Lambda的好奇所以学习了一下,看能不能通过Lambda来开启新的更用效的代码编写方式。以下是通过Lambda实现一个通用型的递归控制处理函数。这只是一个参考,实际上你可以挖掘Lambda更多的使用方法.多谢 装配脑袋 指出问题重新调整一下代码(这里本意不是描述该方法功能如何,而是想体现Lambda的引发的虽一种编码方式).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace lambdaTest
{
public delegate void EventExecute<T>(T source, Program.Recursive<T> next);
public static class Program
{
static void Main(string[] args)
{
UnderList ul = new UnderList();
ul.EmployeeID = 2;
string sql = "select EmployeeID,FirstName from Employees where ReportsTo={0}";
using (System.Data.SqlClient.SqlConnection conn
= new System.Data.SqlClient.SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"))
{
conn.Open();
Do<UnderList>(ul, p => (p.EmployeeID > 0),
delegate(UnderList source, Program.Recursive<UnderList> r)
{
source.Format += "\t";
List<int> mUnders = new List<int>();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = string.Format(sql, source.EmployeeID);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
mUnders.Add((int)reader[0]);
Console.WriteLine(source.Format + reader[1]);
}
}
source.EmployeeID = 0;
foreach (int item in mUnders)
{
source.EmployeeID = item;
r.Do();
}
}
);
}
Console.Read();
}
private static void Do<T>(T i, Func<T, bool> a, EventExecute<T> e)
{
Program.Recursive<T> rec = new Recursive<T>(i,e,a);
e(i, rec);
}
public class Recursive<T>
{
public Recursive(T source, EventExecute<T> e, Func<T, bool> expression)
{
Source = source;
E = e;
Expression = expression;
}
private T Source;
private EventExecute<T> E;
Func<T, bool> Expression;
public void Do()
{
if (Expression(Source))
E(Source, this);
}
}
}
public class UnderList
{
private int mEmployeeID;
public int EmployeeID
{
get
{
return mEmployeeID;
}
set
{
mEmployeeID = value;
}
}
public string Format = "";
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace lambdaTest
{
public delegate void EventExecute<T>(T source, Program.Recursive<T> next);
public static class Program
{
static void Main(string[] args)
{
UnderList ul = new UnderList();
ul.EmployeeID = 2;
string sql = "select EmployeeID,FirstName from Employees where ReportsTo={0}";
using (System.Data.SqlClient.SqlConnection conn
= new System.Data.SqlClient.SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"))
{
conn.Open();
Do<UnderList>(ul, p => (p.EmployeeID > 0),
delegate(UnderList source, Program.Recursive<UnderList> r)
{
source.Format += "\t";
List<int> mUnders = new List<int>();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = string.Format(sql, source.EmployeeID);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
mUnders.Add((int)reader[0]);
Console.WriteLine(source.Format + reader[1]);
}
}
source.EmployeeID = 0;
foreach (int item in mUnders)
{
source.EmployeeID = item;
r.Do();
}
}
);
}
Console.Read();
}
private static void Do<T>(T i, Func<T, bool> a, EventExecute<T> e)
{
Program.Recursive<T> rec = new Recursive<T>(i,e,a);
e(i, rec);
}
public class Recursive<T>
{
public Recursive(T source, EventExecute<T> e, Func<T, bool> expression)
{
Source = source;
E = e;
Expression = expression;
}
private T Source;
private EventExecute<T> E;
Func<T, bool> Expression;
public void Do()
{
if (Expression(Source))
E(Source, this);
}
}
}
public class UnderList
{
private int mEmployeeID;
public int EmployeeID
{
get
{
return mEmployeeID;
}
set
{
mEmployeeID = value;
}
}
public string Format = "";
}
}