using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace LuanmdaTest
{
class Program
{
static void Main(string[] args)
{
//1.DataTable 扩展方法的使用可以减少我们经常去通过循环遍历数据 产生一定的性能问题.
//2.代码量较少.程序简洁.简单.
//3.DataTable 转对象方法, 项目开发很实用,通过DataTable 数据转实体对象集合.
DataTable dt = new Program().DtDate();
//Select找所有符合条件的数据
var rownumber = dt.AsEnumerable().Where(C => C["UserName"].ToString() == "C#高级编程");
// DataTable 模糊查询
var likeSelct = dt.AsEnumerable().Where(C => C["UserName"].ToString().Equals("C#高级编程"));
//DataTable 多条件查询 第一条数据
var rowFirst = dt.AsEnumerable().FirstOrDefault(C => C["UserName"].ToString() == "C#高级编程" && C["UserName"].ToString() != "22");
//DataTable 多条件查询 最后一条数据
var rowLast = dt.AsEnumerable().LastOrDefault(C => C["UserName"].ToString() == "C#高级编程" && C["UserName"].ToString() != "22");
//DataTable 通过Lanmada表达式排序
var rowOrder = dt.AsEnumerable().OrderBy(C => C["UserName"].ToString());
//DataTable 查询排序
string searchString = " UserName='C#高级编程' ";
List<DataRow> listRow = dt.Select(searchString, "UserPwd asc").ToList();
//DataTable 排序
DataView dtView = dt.DefaultView;
dtView.Sort = "ID asc";
dtView.ToTable();
//DataTable 转 List<对象> 公共方法 ★项目开发常用
List<Student> listStudent = DataTableExtensions.ToList<Student>(dt);
foreach (Student item in listStudent)
{
Console.WriteLine(item.StuName);
Console.WriteLine(item.StuAge);
}
Console.ReadLine();
}
public DataTable DtDate()
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("StuName", System.Type.GetType("System.String"));
DataColumn dc1 = new DataColumn("StuAge", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
DataRow dr = dt.NewRow();
dr["StuName"] = "C#高级编程";
dr["StuAge"] = "234";
dt.Rows.Add(dr);
DataRow drA = dt.NewRow();
drA["StuName"] = "C#高级编程";
drA["StuAge"] = "567";
dt.Rows.Add(drA);
DataRow drIn = dt.NewRow();
drIn["StuName"] = "C#高级编程AA";
drIn["StuAge"] = "123";
dt.Rows.Add(drIn);
return dt;
}
}
public static class DataTableExtensions
{
public static List<T> ToList<T>(this DataTable dt) where T : new()
{
//where T : new() :是为了能够new T() 这个对象,不然会报错;
var list = new List<T>();
if (dt == null) return list;
var len = dt.Rows.Count;
for (var i = 0; i < len; i++)
{
var info = new T();
foreach (DataColumn dc in dt.Rows[i].Table.Columns)
{
var field = dc.ColumnName;
var value = dt.Rows[i][field].ToString();
if (string.IsNullOrEmpty(value)) continue;
if (IsDate(value))
{
value = DateTime.Parse(value).ToString();
}
var p = info.GetType().GetProperty(field);
try
{
if (p.PropertyType == typeof(string))
{
p.SetValue(info, value, null);
}
else if (p.PropertyType == typeof(int))
{
p.SetValue(info, int.Parse(value), null);
}
else if (p.PropertyType == typeof(bool))
{
p.SetValue(info, bool.Parse(value), null);
}
else if (p.PropertyType == typeof(DateTime))
{
p.SetValue(info, DateTime.Parse(value), null);
}
else if (p.PropertyType == typeof(float))
{
p.SetValue(info, float.Parse(value), null);
}
else if (p.PropertyType == typeof(double))
{
p.SetValue(info, double.Parse(value), null);
}
else
{
p.SetValue(info, value, null);
}
}
catch (Exception)
{
//p.SetValue(info, ex.Message, null);
}
}
list.Add(info);
}
dt.Dispose(); dt = null;
return list;
}
private static bool IsDate(string d)
{
DateTime d1;
double d2;
return !double.TryParse(d, out d2) && DateTime.TryParse(d, out d1);
}
}
}