none
datatable取值问题? RRS feed

  • 问题

  • 在datatable中有如下数据:
    name          nextname



    A                  B
    B                  C
    C                  D
    E                  F
    D                  E

    我要遍历datatable,并将数据存入List中,存入的条件为:
    首先将第一条存入list,在根据第一条的nextname字段的值(如B)寻找到下一条在name这段中有B的存入list,依次类推,该如何做,谢谢???????

    2009年8月21日 5:25

答案

  • using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Collections.Generic;
    
    public partial class Default4 : System.Web.UI.Page
    {
        public DataTable XTable
        {
            get
            {
                if (Session["XTable"] == null)
                {
                    DataTable fDataTable = new DataTable("XTable");
    
                    fDataTable.Columns.Add("Name", typeof(String));
                    fDataTable.Columns.Add("NextName", typeof(String));
    
                    fDataTable.Rows.Add(new object[] { "A", "B" });
                    fDataTable.Rows.Add(new object[] { "B", "C" });
                    fDataTable.Rows.Add(new object[] { "C", "D" });
                    fDataTable.Rows.Add(new object[] { "D", "E" });
                    fDataTable.Rows.Add(new object[] { "E", "F" });
    
                    Session["XTable"] = fDataTable;
                }
    
                return (DataTable)Session["XTable"];
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            List<String> fList = new List<string>();
    
            this.BuildListByName(fList, "A"); // 任意传一个 Name 或 NextName 存在的值
            //this.BuildListByName(fList, "C");
            //this.BuildListByName(fList, "D");
    
            foreach (String s in fList)
                Response.Write(String.Format("{0}</br>", s));
        }
    
        /// <summary>
        /// 构造 List<String> 对象
        /// </summary>
        /// <param name="fList">需要构造的 List 对象</param>
        /// <param name="fNameOrNextName">需要过滤的 Name 或 NextName 值</param>
        private void BuildListByName(List<String> fList, String fNameOrNextName)
        {
            String filter = (fList.Count == 0)
                ? String.Format("Name ='{0}' OR NextName = '{0}'", fNameOrNextName)
                : String.Format("Name ='{0}'", fNameOrNextName);
    
            foreach (DataRow fCurRow in this.XTable.Select(filter))
            {
                String vals = String.Format("Name:{0}, NextName:{1}", fCurRow[0], fCurRow[1]);
    
                if (!fList.Contains(vals))
                {
                    fList.Add(vals);
                    this.BuildListByName(fList, (String)fCurRow[1]);
                }
            }
        }
    }

    知识改变命运,奋斗成就人生!
    2009年8月21日 5:56
    版主

全部回复

  • using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Collections.Generic;
    
    public partial class Default4 : System.Web.UI.Page
    {
        public DataTable XTable
        {
            get
            {
                if (Session["XTable"] == null)
                {
                    DataTable fDataTable = new DataTable("XTable");
    
                    fDataTable.Columns.Add("Name", typeof(String));
                    fDataTable.Columns.Add("NextName", typeof(String));
    
                    fDataTable.Rows.Add(new object[] { "A", "B" });
                    fDataTable.Rows.Add(new object[] { "B", "C" });
                    fDataTable.Rows.Add(new object[] { "C", "D" });
                    fDataTable.Rows.Add(new object[] { "D", "E" });
                    fDataTable.Rows.Add(new object[] { "E", "F" });
    
                    Session["XTable"] = fDataTable;
                }
    
                return (DataTable)Session["XTable"];
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            List<String> fList = new List<string>();
    
            this.BuildListByName(fList, "A"); // 任意传一个 Name 或 NextName 存在的值
            //this.BuildListByName(fList, "C");
            //this.BuildListByName(fList, "D");
    
            foreach (String s in fList)
                Response.Write(String.Format("{0}</br>", s));
        }
    
        /// <summary>
        /// 构造 List<String> 对象
        /// </summary>
        /// <param name="fList">需要构造的 List 对象</param>
        /// <param name="fNameOrNextName">需要过滤的 Name 或 NextName 值</param>
        private void BuildListByName(List<String> fList, String fNameOrNextName)
        {
            String filter = (fList.Count == 0)
                ? String.Format("Name ='{0}' OR NextName = '{0}'", fNameOrNextName)
                : String.Format("Name ='{0}'", fNameOrNextName);
    
            foreach (DataRow fCurRow in this.XTable.Select(filter))
            {
                String vals = String.Format("Name:{0}, NextName:{1}", fCurRow[0], fCurRow[1]);
    
                if (!fList.Contains(vals))
                {
                    fList.Add(vals);
                    this.BuildListByName(fList, (String)fCurRow[1]);
                }
            }
        }
    }

    知识改变命运,奋斗成就人生!
    2009年8月21日 5:56
    版主
  • 可不可以把第一行也显示出来,谢谢
    2009年8月21日 7:23