书籍,书籍集合,从书籍集合中查找符合条件的书籍
(一)书籍
public
class
Book
{
public int Unid { get ; set ; }
public string Name { get ; set ; }
public int Price { get ; set ; }
public string Publisher { get ; set ; }
}
{
public int Unid { get ; set ; }
public string Name { get ; set ; }
public int Price { get ; set ; }
public string Publisher { get ; set ; }
}
(二)书籍集合
public
static
class
BookHelper
{
public static IList < Book > SearchBook(IList < Book > list)
{
IList < Book > _list = new List < Book > ();
foreach (Book book in list)
{
if (book.Price < 100 )
_list.Add(book);
}
return _list;
}
}
{
public static IList < Book > SearchBook(IList < Book > list)
{
IList < Book > _list = new List < Book > ();
foreach (Book book in list)
{
if (book.Price < 100 )
_list.Add(book);
}
return _list;
}
}
从书籍集合中查找价格在100以下的书籍。这里采用了扩展方法。所以可以以
BookHelper.SearchBook的方式来调用方法
(三)测试
(1)准备
IList
<
Book
>
_list
=
new
List
<
Book
>
();
[SetUp]
public void Init()
{
_list.Add( new Book { Price = 20 , Name = " book1 " });
_list.Add( new Book { Price = 320 , Name = " book6 " });
_list.Add( new Book { Price = 200 , Name = " book2 " });
_list.Add( new Book { Price = 80 , Name = " book3 " });
_list.Add( new Book { Price = 870 , Name = " book4 " });
_list.Add( new Book { Price = 250 , Name = " book5 " });
}
[SetUp]
public void Init()
{
_list.Add( new Book { Price = 20 , Name = " book1 " });
_list.Add( new Book { Price = 320 , Name = " book6 " });
_list.Add( new Book { Price = 200 , Name = " book2 " });
_list.Add( new Book { Price = 80 , Name = " book3 " });
_list.Add( new Book { Price = 870 , Name = " book4 " });
_list.Add( new Book { Price = 250 , Name = " book5 " });
}
(2)测试
[Test(Description
=
"
原始方法
"
)]
public void TestOrigin()
{
List < Book > _newList = BookHelper.SearchBook(_list) as List < Book > ;
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
public void TestOrigin()
{
List < Book > _newList = BookHelper.SearchBook(_list) as List < Book > ;
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
(四)现在把价格判断分离到独立的方法
public
static
IList
<
Book
>
SearchBook1(IList
<
Book
>
list)
{
IList < Book > _list = new List < Book > ();
foreach (Book book in list)
{
if (CheckCondition(book))
_list.Add(book);
}
return _list;
}
public static bool CheckCondition(Book book)
{
if (book.Price < 100 )
return true ;
return false ;
}
{
IList < Book > _list = new List < Book > ();
foreach (Book book in list)
{
if (CheckCondition(book))
_list.Add(book);
}
return _list;
}
public static bool CheckCondition(Book book)
{
if (book.Price < 100 )
return true ;
return false ;
}
(五)测试
[Test(Description
=
"
分离判断方法
"
)]
public void TestOrigin1()
{
List < Book > _newList = BookHelper.SearchBook1(_list) as List < Book > ;
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
public void TestOrigin1()
{
List < Book > _newList = BookHelper.SearchBook1(_list) as List < Book > ;
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
(六)通过委托来传递判断方法
public
delegate
bool
DCheckCondition(Book book);
/// <summary>
/// 传递分离的判断方法
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static IList < Book > SearchBook2(IList < Book > list,DCheckCondition check)
{
IList < Book > _list = new List < Book > ();
foreach (Book book in list)
{
if (check(book))
_list.Add(book);
}
return _list;
}
/// <summary>
/// 传递分离的判断方法
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static IList < Book > SearchBook2(IList < Book > list,DCheckCondition check)
{
IList < Book > _list = new List < Book > ();
foreach (Book book in list)
{
if (check(book))
_list.Add(book);
}
return _list;
}
(七)测试
[Test(Description
=
"
传递分离的判断方法
"
)]
public void TestOrigin2()
{
List < Book > _newList = BookHelper.SearchBook2(_list,BookHelper.CheckCondition) as List < Book > ;
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
public void TestOrigin2()
{
List < Book > _newList = BookHelper.SearchBook2(_list,BookHelper.CheckCondition) as List < Book > ;
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
(八)自定义判断方法
[Test(Description
=
"
代理传递分离的判断方法
"
)]
public void TestOrigin3()
{
List < Book > _newList = BookHelper.SearchBook2(_list,
new LOrigin.BookHelper.DCheckCondition(CheckCondition)) as List < Book > ;
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
public bool CheckCondition(Book book)
{
if (book.Price < 100 )
return true ;
return false ;
}
public void TestOrigin3()
{
List < Book > _newList = BookHelper.SearchBook2(_list,
new LOrigin.BookHelper.DCheckCondition(CheckCondition)) as List < Book > ;
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
public bool CheckCondition(Book book)
{
if (book.Price < 100 )
return true ;
return false ;
}
(九)使用匿名方法来替换
[Test(Description
=
"
匿名方法
"
)]
public void TestOrigin4()
{
IList < Book > _newList = BookHelper.SearchBook2(_list,
delegate (Book book) { return book.Price < 100 ; });
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
public void TestOrigin4()
{
IList < Book > _newList = BookHelper.SearchBook2(_list,
delegate (Book book) { return book.Price < 100 ; });
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
(十)匿名方法符号化
[Test(Description
=
"
匿名方法的符号化
"
)]
public void TestOrigin5()
{
IList < Book > _newList = BookHelper.SearchBook2(_list,book => book.Price < 100 );
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
public void TestOrigin5()
{
IList < Book > _newList = BookHelper.SearchBook2(_list,book => book.Price < 100 );
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
(十一)
[Test(Description
=
"
IList的静态方法Where,并采用goes to
"
)]
public void TestOrigin6()
{
IList < Book > _newList = _list.Where(book => book.Price < 100 ).ToArray < Book > ();
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
public void TestOrigin6()
{
IList < Book > _newList = _list.Where(book => book.Price < 100 ).ToArray < Book > ();
foreach (Book book in _newList)
Console.WriteLine(book.Name);
}
不管是IList还是IList都可以使用这个Where方法了但是做集合操作的时候我们不仅仅需要Where,还需要OrderBy,Group等等,我想把所有的SQL能干的都移植过来。当然微软也意识到了这点,如是在.net 3.5里,微软发布了我们梦寐以求的Linq,将查询集成到语言里面来。它给IEnumerable添加了很多扩展方法,这样你可以很自然的去调用。你可以使用Reflector打开System.Core.dll,打开System.Linq命名空间,在这个命名空间里有一个Enumerable类,这里面就是微软为我们添加的扩展方法,看看,是不是SQL里所有的东西都可以在这里找到了
更多内容请见:http://kb.cnblogs.com/zt/linq/