• Dataset,DataTable


    private void BiudTree()
    {
    MmyeeAd.BLL.ADManage.AdClass bll = new MmyeeAd.BLL.ADManage.AdClass();
    DataTable dt = bll.GetAllList().Tables[0];
    this.dropParent.Items.Clear();
    //加载树
    this.dropParent.Items.Add(new ListItem("顶级目录", "0"));
    DataRow[] drs = dt.Select("ParentId= 0");
    foreach (DataRow r in drs)
    {
    string nodeid = r["ClassId"].ToString();
    string text = r["ClassDesc"].ToString();
    string parentid = r["ParentId"].ToString();
    text = "╋" + text;
    this.dropParent.Items.Add(new ListItem(text, nodeid));
    int sonparentid = int.Parse(nodeid);
    string blank = "├";
    BindNode(sonparentid, dt, blank);
    }
    this.dropParent.DataBind();

    }
    private void BindNode(int parentid, DataTable dt, string blank)
    {
    DataRow[] drs = dt.Select("ParentID= " + parentid);

    foreach (DataRow r in drs)
    {
    string nodeid = r["ClassId"].ToString();
    string text = r["ClassDesc"].ToString();
    text = blank + "『" + text + "』";
    this.dropParent.Items.Add(new ListItem(text, nodeid));
    int sonparentid = int.Parse(nodeid);
    string blank2 = blank + "─";

    BindNode(sonparentid, dt, blank2);
    }
    }

    public DataTable test(int id) {

    var dt = GetLanguageDataTable(pfAppId);
    dt.AsEnumerable().ToList().ForEach(p => { mm m = new mm(); var lang = p.Field<string>("languageName"); p.SetField<string>("languageName", m.GetProperLanguage(lang)); });
    return dt;
    }


    var path = Server.MapPath("~/App_Data/ff.txt");
    var invalidEMails = MailHelper.GetInvalidEMailList(path);
    dt.AsEnumerable().ToList().ForEach(p => {
    var email = p.Field<string>("ff");
    if (invalidEMails.FirstOrDefault(w => w.ToLower() == email.ToLower())!=null) {
    dt.Rows.Remove(p);
    }
    });

    DataTable dts;
    DataRow[] foundRow;
    foundRow = dts.Select("ID=99", "");
    foreach (DataRow row in foundRow)
    {
    dts.Rows.Remove(row);

    public class HomeController : BaseController {
    public class Student {
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public string Address { get; set; }
    public string MobileNo { get; set; }
    public int? Status { get; set; }
    }
    public static List<T> ConvertDataTableToList<T>(DataTable dt) {
    List<T> data = new List<T>();
    foreach (DataRow row in dt.Rows) {
    T item = GetItem<T>(row);
    data.Add(item);
    }
    return data;
    }
    private static T GetItem<T>(DataRow dr) {
    Type temp = typeof(T);
    T obj = Activator.CreateInstance<T>();

    foreach (DataColumn column in dr.Table.Columns) {
    foreach (PropertyInfo pro in temp.GetProperties()) {
    if (pro.Name == column.ColumnName){
    var propertyValue = dr[column.ColumnName];
    //pro.SetValue(obj, dr[column.ColumnName], null);
    if (!pro.PropertyType.IsGenericType) {
    pro.SetValue(obj, propertyValue == DBNull.Value ? null : Convert.ChangeType(propertyValue, pro.PropertyType));
    } else {
    Type genericTypeDefinition = pro.PropertyType.GetGenericTypeDefinition();
    if (genericTypeDefinition == typeof(Nullable<>)) {
    pro.SetValue(obj, propertyValue==DBNull.Value ? null : Convert.ChangeType(propertyValue, Nullable.GetUnderlyingType(pro.PropertyType)));
    }
    }
    }
    else
    continue;
    }
    }
    return obj;
    }

    public DataTable GetDataTable() {
    DataTable dt = new DataTable("Student");
    dt.Columns.Add("StudentId", typeof(Int32));
    dt.Columns.Add("StudentName", typeof(string));
    dt.Columns.Add("Address", typeof(string));
    dt.Columns.Add("MobileNo", typeof(string));
    dt.Columns.Add("Status", typeof(Int32));
    //Data
    dt.Rows.Add(1, "Manish", "Hyderabad","0000000000");
    dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111");
    dt.Rows.Add(3, "Namit", "Pune", "1222222222");
    dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333",1);


    return dt;
    }

    public DataSet GetDatasets() {

    DataSet dsClass = new DataSet();
    //创建班级表
    DataTable dtClass = new DataTable("Class");
    //创建班级名称列
    DataColumn dcClassName = new DataColumn("ClassName",typeof(string));
    dcClassName.MaxLength = 50;
    //创建年级ID列
    DataColumn dcGradeID = new DataColumn("GradeID",typeof(int));
    //将定义好列添加到班级表中
    dtClass.Columns.Add(dcClassName);
    dtClass.Columns.Add(dcGradeID);
    //创建一个新的数据行
    DataRow drClass = dtClass.NewRow();
    drClass["className"] = "1";
    drClass["gradeID"] = 1;
    //将新的数据行插入班级表中
    dtClass.Rows.Add(drClass);
    //将班级表添加到DataSet中
    dsClass.Tables.Add(dtClass);
    return dsClass;
    }

    public ActionResult Load() {

    var dt = GetDataTable();
    List<Student> target = dt.AsEnumerable()
    .Select(dataRow => new Student {
    StudentName = dataRow.Field<string>("StudentName"),
    Status = dataRow.Field<int?>("Status")
    }).ToList();

    var empList = dt.AsEnumerable().Select(dataRow => new Student {
    StudentName = dataRow.Field<string>("StudentName"),
    Status = dataRow.Field<int?>("Status")
    }).ToList();

    List<Student> studentList = new List<Student>();

    for (int i = 0; i < dt.Rows.Count; i++) {
    Student student = new Student();
    student.StudentId = Convert.ToInt32(dt.Rows[i]["StudentId"]);
    student.StudentName = dt.Rows[i]["StudentName"].ToString();
    student.Address = dt.Rows[i]["Address"].ToString();
    student.MobileNo = dt.Rows[i]["MobileNo"].ToString();
    studentList.Add(student);
    }

    //studentList = (from DataRow dr in dt.Rows
    // select new Student() {
    // StudentId = Convert.ToInt32(dr["StudentId"]),
    // StudentName = dr["StudentName"].ToString(),
    // Address = dr["Address"].ToString(),
    // MobileNo = dr["MobileNo"].ToString()
    // }).ToList();

    var result2 = BaseResponseEntity.Success(new { StudentList1 = GetDatasets(),StudentList = GetDatasets().Tables[0] });
    return JsonContentResult(result2);

    var result1 = BaseResponseEntity.Success(new { StudentList = dt });
    return JsonContentResult(result1);

    studentList = ConvertDataTableToList<Student>(dt);
    var result = BaseResponseEntity.Success(new { StudentList=studentList});
    //return JsonContentResult(studentList);
    return JsonContentResult(result);
    }


    public ActionResult Index() {
    return View();
    }

    public ActionResult About() {
    ViewBag.Message = "Your application description page.";

    return View();
    }

    public ActionResult Contact() {
    ViewBag.Message = "Your contact page.";

    return View();
    }
    }

    http://www.c-sharpcorner.com/UploadFile/ee01e6/different-way-to-convert-datatable-to-list/

    http://www.cnblogs.com/envelope/archive/2010/04/20/1716343.html

    https://www.cnblogs.com/sky-net/p/5956538.html

  • 相关阅读:
    Custom C# MOSS Navigation Using PortalSiteMapProvider
    SharePoint Content Type ID
    SharePoint 2010 migration process
    CAML 用法And 使用
    Posts Tagged Unrecognized attribute ‘allowInsecureTransport’
    程序员的最大缺点
    程序员四大忌,你做到了吗?
    中国程序员哪些缺点或是不足需要改进的 .
    离职需要注意些什么问题
    工作不要只做分内事,对你没什么好处
  • 原文地址:https://www.cnblogs.com/zwei1121/p/8546407.html
Copyright © 2020-2023  润新知