Linq to DataSet 和 DataSet使用方法学习

时间:2021-10-09 20:51:19

简单入门:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; namespace LinqtoDataset
{
class Program
{
static void Main(string[] args)
{
LinqToDataSet.UpdateData();
LinqToDataSet.PrintData(); }
class LinqToDataSet
{
public static DataSet CreatDataSet()
{
string[] nameSet = { "王霞", "张三", "李四", "李花", "王五", "陆六", "夏七", "吴八" };
string[] sexSet = { "女", "男", "男", "女", "男", "男", "男", "男" };
int[] ageSet = { , , , , , , , };
DataSet ds = new DataSet();
DataTable dt = new DataTable("People");
ds.Tables.Add(dt);
dt.Columns.Add(new DataColumn("Name", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Sex", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Age", Type.GetType("System.Int32")));
for (int i = ; i < nameSet.Length; i++)
{
DataRow dr = dt.NewRow();
dr["Name"] = nameSet[i];
dr["Sex"] = sexSet[i];
dr["Age"] = ageSet[i];
dt.Rows.Add(dr);
}
return ds;
}
public static void PrintData()
{
DataSet ds = CreatDataSet();
DataTable dt = ds.Tables["People"];
var query1 = from item in dt.AsEnumerable() select item;
foreach (var item in query1)
{
Console.WriteLine("姓名:{0},性别:{1},年龄:{2}", item["Name"].ToString(), item.Field<string>("Sex"), item.Field<int>("Age"));
}
var query2 = from item in dt.AsEnumerable() select item.Field<string>("Name");
foreach (var item in query2) { Console.WriteLine("姓名:{0}", item); }
var query3 = from item in dt.AsEnumerable() where item.Field<int>("Age") >= select item;
foreach (var item in query3) { Console.WriteLine("姓名:{0},性别:{1},年龄:{2}", item.Field<string>("Name"), item.Field<string>("Sex"), item.Field<int>("Age")); }
Console.ReadLine();
}
public static void UpdateData()
{
DataSet ds = CreatDataSet();
DataTable dt = ds.Tables[];
foreach (var item in dt.AsEnumerable())
{
int age = item.Field<int>("Age");
item.SetField<int>("Age", age + );
}
foreach (var item in dt.AsEnumerable()) { Console.WriteLine("姓名:{0},性别:{1},年龄:{2}", item.Field<string>("Name"), item.Field<string>("Sex"), item.Field<int>("Age")); }
}
}
}
}

(有待整理!)

Linq to DataSet  和 DataSet使用方法学习

http://www.cnblogs.com/guowenhui/archive/2011/12/04/2275484.html

https://msdn.microsoft.com/zh-cn/library/bb399401.aspx