天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

LINQ To DataSet

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

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

下面,我們就來看看一個使用LINQ to DataSet的實例,這個例子主要描述了一下上面擴展方法的用法,同時給出了部分注意的事項:

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);
        
//我們試圖訪問DataTable中數據行的Orginal版本,由于此時還沒有建立原始版本,
        
//所以將導致異常
        Console.WriteLine("We try to get access to original version, so we will get the exception.:");
        PrintStudentTable(originalTable);
        
//我們使用CopyToDataTable來建立DataTable中數據行的Orginal版本
        Console.WriteLine("We will use CopyToDataTable to build original version.");
        DataTable newTable 
= originalTable.AsEnumerable().CopyToDataTable();
        PrintStudentTable(newTable);
        
//使用SetField來更新
        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來設置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來合并,由于我們沒有指定表的主鍵,
        
//所以只會簡單的追加在目標數據表的最后
        Console.WriteLine("After call CopyToDataTable.We will not get our expected result because we have not set primary key.");
        
//首先,我們調用AcceptChanges來建立Original版本,否則,避免顯示時拋出異常
        originalTable.AcceptChanges();
       newTable.AsEnumerable().CopyToDataTable(originalTable, LoadOption.OverwriteChanges);
        PrintStudentTable(originalTable);
        
//我們使用Distinct來去掉剛才重復的記錄,由于此時我們沒有使用DatarowComparer.Default
        
//所以我們將得不到我們想要的結果
        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來去掉剛才重復的記錄,使用DatarowComparer.Default比較器
        
//所以我們將得到我們想要的結果
        Console.WriteLine("After call Distinct.this is what we want.");
       distinctTable
=originalTable.AsEnumerable().Distinct(DataRowComparer.Default);
        PrintStudentTable(distinctTable);
        
//我們先設置主鍵,然后再使用CopyToDataTable來合并
        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);     
    }

}


這個例子的輸出結果為:

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知識庫LINQ To DataSet,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 日本美女抠逼 | 色偷偷网站 | 国产又粗又猛又爽又黄的免费视频 | 亚洲日韩国产成网站在线 | 一二三四在线高清中文版免费观看电影 | 久久久久国产一级毛片高清片 | 手机在线观看毛片 | 亚洲AV美女成人网站P站 | 女人高潮久久久叫人喷水 | 国产亚洲精品久久久久久一区二区 | 亚洲免费视频在线观看 | 男人狂躁进女人免费视频公交 | 99精品在线看 | JIZZ19学生第一次 | 97国产人妻精品无码AV在线 | 国产欧美精品一区二区色综合 | 日本一卡二卡三卡四卡无卡免费播放 | 浪潮AV色综合久久天堂 | 久久精品一区二区影院 | 国产69精品久久久久麻豆 | 亚洲欧美高清在线 | 人人澡人人擦人人免费 | 男人插曲女人下生免费大全 | 国产制服丝袜91在线 | 在线观看中文字幕码2021不用下载 | 亚洲色图激情文学 | 国产成人免费高清在线观看 | 伊人精品影院 | 国产精品久久人妻无码网站一区L | 亚洲电影网址 | 无码专区久久综合久综合字幕 | 国产精一品亚洲二区在线播放 | 超嫩校花被灌醉在线观看 | 在教室伦流澡到高潮HNP视频 | 真实国产乱子伦精品一区二区三区 | 揉抓捏打抽插射免费视频 | 日韩欧美视频一区二区 | 青春草国产成人精品久久 | 狠狠色狠狠色综合 | 亚洲免费久久 | 在线免费观看国产精品 |