文/黄忠成
在【极意之道-次世代.NET Framework 3.5资料库开发圣典】(好长的书名,我该取个匿称吗?哈)一书第三章中,有下面这段叙述:
书中列出一小段程式码,证明事实上只要是C# 2.0,就能够使用LINQ所提供的查询函式,前提是电脑中必须安装.NET Framework 3.5。
那好,什么时候我们会需要这种写法呢?想像一下,你可以利用LINQ查询函式来做什么?你可以将两个阵列Join起来、排序、Group、查询,你也可以 将两个XML档案Join起来,甚至可以将两个DataTable Join起来,哦!这段叙述是否挑到你最敏感的那条神经了呢?
不过, 说归说,做归做,如果在.NET Framework 2.0中叫用LINQ,能得到什么好处呢?很明显的,你原本的专案不需要做大幅度的升级,你不需要装Visual Studio 2008,你只需安装.NET Framework 3.5,然后加入对System.Core的参考,就能得到LINQ查询函式的火力,再加入System.Xml.Linq的参考,就能得到LINQ To XML查询函式,加入System.Data.DataSetExtensions的参考,就能将两个DataTable Join起来。
但,有了这些好处,事情真的会比较简单吗?那得视情况而定,在没有LINQ Expression支援下使用LINQ查询函式,有时会将事情变的复杂,换句话说,程式码可能变多了。不过,大部份情况下,我相信使用LINQ查询函式 会把程式码缩短,毕竟,查询演算法是内建在LINQ中了。
上 面这段程式码可以运行于Visual Studio 2005、Visual Studio 2008,如你使用Visual Studio 2005,请先安装.NET Framework 3.5,然后于专案中添加对System.Core的参考(此DLL位于Program Files/ Reference Assemblies/Microsoft/Framework/3.5),完成后编译程式,你便达到于C#,Visual Studio 2005中使用LINQ的目的。
Join 2 DataTables
我想有许多写Windows Form程式的人,都曾经对于如何Join两个DataTable困扰过,事实上这很简单,只要建另一个DataTable,一个萝卜一个坑的填即可。
不过有了LINQ To DataSet,一切都变的简单且直觉,但!缺少了LINQ Expression的C# 2.0,只凭LINQ查询函式来做,可以把这件事变简单吗?
.........
这个程式需要引用System.Data.DataSetExtnesions.DLL,她与System.Core位于同一目录下,JoinTables的函式传回值顷印结果如下:
..........................................
欢迎光临C# 2.0与LINQ的世界。
在【极意之道-次世代.NET Framework 3.5资料库开发圣典】(好长的书名,我该取个匿称吗?哈)一书第三章中,有下面这段叙述:
任何支援.NET Framework
2
.0的程式语言都能够叫用LINQ所提供的查询函式,也就是说能使用C#
2
.0来呼叫LINQ Framework
书中列出一小段程式码,证明事实上只要是C# 2.0,就能够使用LINQ所提供的查询函式,前提是电脑中必须安装.NET Framework 3.5。
那好,什么时候我们会需要这种写法呢?想像一下,你可以利用LINQ查询函式来做什么?你可以将两个阵列Join起来、排序、Group、查询,你也可以 将两个XML档案Join起来,甚至可以将两个DataTable Join起来,哦!这段叙述是否挑到你最敏感的那条神经了呢?
不过, 说归说,做归做,如果在.NET Framework 2.0中叫用LINQ,能得到什么好处呢?很明显的,你原本的专案不需要做大幅度的升级,你不需要装Visual Studio 2008,你只需安装.NET Framework 3.5,然后加入对System.Core的参考,就能得到LINQ查询函式的火力,再加入System.Xml.Linq的参考,就能得到LINQ To XML查询函式,加入System.Data.DataSetExtensions的参考,就能将两个DataTable Join起来。
但,有了这些好处,事情真的会比较简单吗?那得视情况而定,在没有LINQ Expression支援下使用LINQ查询函式,有时会将事情变的复杂,换句话说,程式码可能变多了。不过,大部份情况下,我相信使用LINQ查询函式 会把程式码缩短,毕竟,查询演算法是内建在LINQ中了。
static
void
DemoLinq()
{
string[] list = new string[] { "code6421", "tom", "david", "cathy" };
IEnumerable<string> result = Enumerable.Where<string>(list,
delegate(string item)
{
return item == "tom";
});
Console.WriteLine(Enumerable.FirstOrDefault(result));
}
{
string[] list = new string[] { "code6421", "tom", "david", "cathy" };
IEnumerable<string> result = Enumerable.Where<string>(list,
delegate(string item)
{
return item == "tom";
});
Console.WriteLine(Enumerable.FirstOrDefault(result));
}
上 面这段程式码可以运行于Visual Studio 2005、Visual Studio 2008,如你使用Visual Studio 2005,请先安装.NET Framework 3.5,然后于专案中添加对System.Core的参考(此DLL位于Program Files/ Reference Assemblies/Microsoft/Framework/3.5),完成后编译程式,你便达到于C#,Visual Studio 2005中使用LINQ的目的。
Join 2 DataTables
我想有许多写Windows Form程式的人,都曾经对于如何Join两个DataTable困扰过,事实上这很简单,只要建另一个DataTable,一个萝卜一个坑的填即可。
不过有了LINQ To DataSet,一切都变的简单且直觉,但!缺少了LINQ Expression的C# 2.0,只凭LINQ查询函式来做,可以把这件事变简单吗?
using
System.Data.SqlClient;
........
static IEnumerable < JoinData > JoinTables()
{
using (SqlConnection conn = new SqlConnection(
"Data Source=JEFFRAY;Initial Catalog=Northwind;Integrated Security=True"))
{
SqlDataAdapter adapter1 = new SqlDataAdapter("SELECT * FROM Customers", conn);
SqlDataAdapter adapter2 = new SqlDataAdapter("SELECT * FROM Orders", conn);
DataTable dt1 = new DataTable("Customers");
DataTable dt2 = new DataTable("Orders");
adapter1.Fill(dt1);
adapter2.Fill(dt2);
return Enumerable.Join<DataRow,DataRow,string,JoinData>(
DataTableExtensions.AsEnumerable(dt2),
DataTableExtensions.AsEnumerable(dt1),
delegate(DataRow s1)
{
return s1.IsNull("CustomerID") ?
null : (string)s1["CustomerID"];
},
delegate(DataRow s2)
{
return s2.IsNull("CustomerID") ? null :
(string)s2["CustomerID"];
},
delegate(DataRow s1,DataRow s2)
{
JoinData result = new JoinData();
result.OrderID = s1.IsNull("OrderID") ? -1 :
(int)s1["OrderID"];
result.CompanyName = s2.IsNull("CompanyName") ?
string.Empty : (string)s2["CompanyName"];
return result;
});
}
}
........
static IEnumerable < JoinData > JoinTables()
{
using (SqlConnection conn = new SqlConnection(
"Data Source=JEFFRAY;Initial Catalog=Northwind;Integrated Security=True"))
{
SqlDataAdapter adapter1 = new SqlDataAdapter("SELECT * FROM Customers", conn);
SqlDataAdapter adapter2 = new SqlDataAdapter("SELECT * FROM Orders", conn);
DataTable dt1 = new DataTable("Customers");
DataTable dt2 = new DataTable("Orders");
adapter1.Fill(dt1);
adapter2.Fill(dt2);
return Enumerable.Join<DataRow,DataRow,string,JoinData>(
DataTableExtensions.AsEnumerable(dt2),
DataTableExtensions.AsEnumerable(dt1),
delegate(DataRow s1)
{
return s1.IsNull("CustomerID") ?
null : (string)s1["CustomerID"];
},
delegate(DataRow s2)
{
return s2.IsNull("CustomerID") ? null :
(string)s2["CustomerID"];
},
delegate(DataRow s1,DataRow s2)
{
JoinData result = new JoinData();
result.OrderID = s1.IsNull("OrderID") ? -1 :
(int)s1["OrderID"];
result.CompanyName = s2.IsNull("CompanyName") ?
string.Empty : (string)s2["CompanyName"];
return result;
});
}
}
.........
public
class
JoinData
{
private int _orderID;
private string _companyName;
public int OrderID
{
get
{
return _orderID;
}
set
{
_orderID = value;
}
}
public string CompanyName
{
get
{
return _companyName;
}
set
{
_companyName = value;
}
}
}
{
private int _orderID;
private string _companyName;
public int OrderID
{
get
{
return _orderID;
}
set
{
_orderID = value;
}
}
public string CompanyName
{
get
{
return _companyName;
}
set
{
_companyName = value;
}
}
}
这个程式需要引用System.Data.DataSetExtnesions.DLL,她与System.Core位于同一目录下,JoinTables的函式传回值顷印结果如下:
10779
,Morgenstern Gesundkost
10780 ,LILA - Supermercado
10781 ,Wartian Herkku
10782 ,Cactus Comidas para llevar
10783 ,Hanari Carnes
10784 ,Magazzini Alimentari Riuniti
10785 ,GROSELLA - Restaurante
10786 ,Queen Cozinha
10787 ,La maison d ' Asie
10788 ,QUICK - Stop
10789 ,Folies gourmandes
10790 ,Gourmet Lanchonetes
10791 ,Frankenversand
10792 ,Wolski Zajazd
10793 ,Around the Horn
10794 ,Que Delicia
10795 ,Ernst Handel
10796 ,HILARION - Abastos
10797 ,Drachenblut Delikatessen
10798 ,Island Trading
10799 ,Koniglich Essen
10800 ,Seven Seas Imports
10780 ,LILA - Supermercado
10781 ,Wartian Herkku
10782 ,Cactus Comidas para llevar
10783 ,Hanari Carnes
10784 ,Magazzini Alimentari Riuniti
10785 ,GROSELLA - Restaurante
10786 ,Queen Cozinha
10787 ,La maison d ' Asie
10788 ,QUICK - Stop
10789 ,Folies gourmandes
10790 ,Gourmet Lanchonetes
10791 ,Frankenversand
10792 ,Wolski Zajazd
10793 ,Around the Horn
10794 ,Que Delicia
10795 ,Ernst Handel
10796 ,HILARION - Abastos
10797 ,Drachenblut Delikatessen
10798 ,Island Trading
10799 ,Koniglich Essen
10800 ,Seven Seas Imports
..........................................
欢迎光临C# 2.0与LINQ的世界。