一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

LINQ To DataSet

LINQ to DataSet主要是提供對(duì)離線數(shù)據(jù)的支持,只有在填充DataSet之后,我們才能使用LINQ to DataSet來(lái)查詢數(shù)據(jù)。其功能主要是通過(guò)System.Data.DataRowExtions和System.Data.DataTableExtensions兩個(gè)靜態(tài)類中的擴(kuò)展方法來(lái)公開的。LINQ to DataSet是LINQ to ADO.NET中的一部分,但這部分所占比重非常小,內(nèi)容也比較少。下面就讓我們首先來(lái)看看DataTableExtensions中的擴(kuò)展方法:

public static EnumerableRowCollection<DataRow> AsEnumerable(this DataTable source)
----------------------------------------------------------------------------------------------------
public static DataView AsDataView(this DataTable table)
public static DataView AsDataView<T>(this EnumerableRowCollection<T> source) where T : DataRow
-----------------------------------------------------------------
public static DataTable CopyToDataTable<T>(this IEnumerable<T> source) where T : DataRow
public static void CopyToDataTable<T>(this IEnumerable<T> source,DataTable table,LoadOption options) where T : DataRow
public static void CopyToDataTable<T>(this IEnumerable<T> source,DataTable table,LoadOption options,FillErrorEventHandler errorHandler) where T : DataRow

下面,我們就來(lái)看看一個(gè)使用LINQ to DataSet的實(shí)例,這個(gè)例子主要描述了一下上面擴(kuò)展方法的用法,同時(shí)給出了部分注意的事項(xiàng):

public static class LINQToDataSet
{
    
public class Student
    
{
        
public int Id;
        
public string Name;
    }

    
public static DataTable GetDataTable(Student[] students)
    
{
        DataTable table 
= new DataTable();
        table.Columns.Add(
"Id"typeof(Int32));
        table.Columns.Add(
"Name"typeof(string));
        
foreach (Student student in students)
        
{
            table.Rows.Add(student.Id, student.Name);
        }

        
return (table);
    }

    
public static void PrintStudentTable(DataTable dt)
    
{
        PrintStudentTable(dt.AsEnumerable());
    }

    
public static void PrintStudentTable(IEnumerable<DataRow> dt)
    
{
        Console.WriteLine(
"================================================================");
        
try
        
{
            
foreach (DataRow dataRow in dt.AsEnumerable())
            
{
                Console.WriteLine(
"Student Id = {0} :original {1}:current {2}",
dataRow.Field
<int>("Id"),
dataRow.Field
<string>("Name", DataRowVersion.Original),
dataRow.Field
<string>("Name", DataRowVersion.Current));
            }

        }

        
catch (Exception e)
        
{
            Console.WriteLine(
"Exception:" + e.Message);
        }

        Console.WriteLine(
"================================================================");
    }

    
public static void Test()
    
{
        Student[] students 
= new Student { Id = 1, Name = "Lazy Bee" },
                               
new Student { Id = 7, Name = "Flying Wind" },
                               
new Student { Id = 13, Name = "PiPi Zhu" },
                               
new Student { Id = 72, Name = "Chong Chong" }}
;
        DataTable originalTable 
= GetDataTable(students);
        
//我們?cè)噲D訪問(wèn)DataTable中數(shù)據(jù)行的Orginal版本,由于此時(shí)還沒(méi)有建立原始版本,
        
//所以將導(dǎo)致異常
        Console.WriteLine("We try to get access to original version, so we will get the exception.:");
        PrintStudentTable(originalTable);
        
//我們使用CopyToDataTable來(lái)建立DataTable中數(shù)據(jù)行的Orginal版本
        Console.WriteLine("We will use CopyToDataTable to build original version.");
        DataTable newTable 
= originalTable.AsEnumerable().CopyToDataTable();
        PrintStudentTable(newTable);
        
//使用SetField來(lái)更新
        Console.WriteLine("After call SetField to change name.");
        (from s 
in newTable.AsEnumerable()
         
where s.Field<string>("Name"== "PiPi Zhu"
         select s).Single
<DataRow>().SetField("Name""George Oscar Bluth");
        PrintStudentTable(newTable);
        
//使用SetField來(lái)設(shè)置null
        Console.WriteLine("After call SetField to change name to null.");
        (from s 
in newTable.AsEnumerable()
         
where s.Field<int>("Id"== 13
         select s).Single
<DataRow>().SetField<string>("Name"null);
        PrintStudentTable(newTable);
        
//使用CopyToDataTable來(lái)合并,由于我們沒(méi)有指定表的主鍵,
        
//所以只會(huì)簡(jiǎn)單的追加在目標(biāo)數(shù)據(jù)表的最后
        Console.WriteLine("After call CopyToDataTable.We will not get our expected result because we have not set primary key.");
        
//首先,我們調(diào)用AcceptChanges來(lái)建立Original版本,否則,避免顯示時(shí)拋出異常
        originalTable.AcceptChanges();
       newTable.AsEnumerable().CopyToDataTable(originalTable, LoadOption.OverwriteChanges);
        PrintStudentTable(originalTable);
        
//我們使用Distinct來(lái)去掉剛才重復(fù)的記錄,由于此時(shí)我們沒(méi)有使用DatarowComparer.Default
        
//所以我們將得不到我們想要的結(jié)果
        Console.WriteLine("After call Distinct.We will not get our expected result because we have not used DatarowComparer.Default comparer.");
        IEnumerable
<DataRow> distinctTable=originalTable.AsEnumerable().Distinct();
        PrintStudentTable(distinctTable);
        
//我們使用Distinct來(lái)去掉剛才重復(fù)的記錄,使用DatarowComparer.Default比較器
        
//所以我們將得到我們想要的結(jié)果
        Console.WriteLine("After call Distinct.this is what we want.");
       distinctTable
=originalTable.AsEnumerable().Distinct(DataRowComparer.Default);
        PrintStudentTable(distinctTable);
        
//我們先設(shè)置主鍵,然后再使用CopyToDataTable來(lái)合并
        Console.WriteLine("After call CopyToDataTable.this is what we want.");
        originalTable 
= GetDataTable(students);
        originalTable.PrimaryKey 
= new DataColumn[] { originalTable.Columns["Id"] }
        newTable.AsEnumerable().CopyToDataTable(originalTable, LoadOption.OverwriteChanges);
        PrintStudentTable(originalTable);     
    }

}


這個(gè)例子的輸出結(jié)果為:

We try to get access to original version, so we will get the exception.:
================================================================
Exception:There 
is no Original data to access.
================================================================
We will use CopyToDataTable to build original version.
================================================================
Student Id 
= 1 :original Lazy Bee:current Lazy Bee
Student Id 
= 7 :original Flying Wind:current Flying Wind
Student Id 
= 13 :original PiPi Zhu:current PiPi Zhu
Student Id 
= 72 :original Chong Chong:current Chong Chong
================================================================
After call SetField to change name.
================================================================
Student Id 
= 1 :original Lazy Bee:current Lazy Bee
Student Id 
= 7 :original Flying Wind:current Flying Wind
Student Id 
= 13 :original PiPi Zhu:current George Oscar Bluth
Student Id 
= 72 :original Chong Chong:current Chong Chong
================================================================
After call SetField to change name to 
null.
================================================================
Student Id 
= 1 :original Lazy Bee:current Lazy Bee
Student Id 
= 7 :original Flying Wind:current Flying Wind
Student Id 
= 13 :original PiPi Zhu:current
Student Id 
= 72 :original Chong Chong:current Chong Chong
================================================================
After call CopyToDataTable.We will not 
get our expected result because we have not set primary key.
================================================================
Student Id 
= 1 :original Lazy Bee:current Lazy Bee
Student Id 
= 7 :original Flying Wind:current Flying Wind
Student Id 
= 13 :original PiPi Zhu:current PiPi Zhu
Student Id 
= 72 :original Chong Chong:current Chong Chong
Student Id 
= 1 :original Lazy Bee:current Lazy Bee
Student Id 
= 7 :original Flying Wind:current Flying Wind
Student Id 
= 13 :original :current
Student Id 
= 72 :original Chong Chong:current Chong Chong
================================================================
After call Distinct.We will not 
get our expected result because we have not used DatarowComparer.Default comparer.
================================================================
Student Id 
= 1 :original Lazy Bee:current Lazy Bee
Student Id 
= 7 :original Flying Wind:current Flying Wind
Student Id 
= 13 :original PiPi Zhu:current PiPi Zhu
Student Id 
= 72 :original Chong Chong:current Chong Chong
Student Id 
= 1 :original Lazy Bee:current Lazy Bee
Student Id 
= 7 :original Flying Wind:current Flying Wind
Student Id 
= 13 :original :current
Student Id 
= 72 :original Chong Chong:current Chong Chong
================================================================
After call Distinct.
this is what we want.
================================================================
Student Id 
= 1 :original Lazy Bee:current Lazy Bee
Student Id 
= 7 :original Flying Wind:current Flying Wind
Student Id 
= 13 :original PiPi Zhu:current PiPi Zhu
Student Id 
= 72 :original Chong Chong:current Chong Chong
Student Id 
= 13 :original :current
================================================================
After call CopyToDataTable.
this is what we want.
================================================================
Student Id 
= 1 :original Lazy Bee:current Lazy Bee
Student Id 
= 7 :original Flying Wind:current Flying Wind
Student Id 
= 13 :original :current
Student Id 
= 72 :original Chong Chong:current Chong Chong
================================================================

it知識(shí)庫(kù)LINQ To DataSet,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 久久成人免费观看全部免费 | 三级欧美 | 国产成人免费视频 | 国模337人人本艺术150p | 成人视屏在线 | 国产一二三区视频 | 人人干人人爽 | 国产精品99精品久久免费 | 色婷亚洲 | 亚洲精品影院 | 久久国产香蕉 | 日韩中文字幕在线免费观看 | 欧美成人观看 | 激情婷婷成人亚洲综合 | 久爱精品视频在线视频 | 国产婷婷色综合成人精品 | 色多多在线观看视频 | 久久露脸国产精品 | 午夜视频网 | 美女把屁股扒开让男人桶视频 | 国产一区二区在免费观看 | 色爱区综合激情五月综合色 | 亚洲一区成人 | 91成人影院未满十八勿入 | 欧美中文小说在线观看 | 国产中文字幕在线视频 | 国产精品高清全国免费观看 | 色哟哟哟在线观看www | 亚洲综合亚洲综合网成人 | 国产精品视频一区二区噜噜 | a一级一色一情 | 中文字幕123| 日本亚洲精品 | 51国产偷自视频区视频手机播器 | 国产精品欧美亚洲韩国日本久久 | 四虎现在的网址入口 | 国产精品四虎在线观看免费 | 亚洲丝袜天堂 | 久久毛片久久毛 | 涩涩涩涩涩涩涩涩涩涩 | 久久国产精品-国产精品 |