简体   繁体   中英

How to get each value from a particular mysql table by using datatable without using a for or foreach loop in c#?

I need to get each value from mysql table by using DataTable without using a loop. Here I have two DataTables , I want to increment dtfilmsngtemp and need to get values, ie if dtsnglyric have id=2 , Then I need to have get id=2 in dtfilmsngtemp , So in general I need to get he same 'id's' from two DataTables . First of all row id is 1 in dtsnglyric and dtfilmsngtemp , But dtsnglyric is incremented to 2, According to my requirement dtfilmsngtemp is also need to become 2. How it is possible?

DataTable dtsnglyric = GetAllsnglyrctmp();
DataTable dtfilmsngtemp = GetAllfilmsngtemp();
foreach (DataRow drow1 in dtsnglyric.Rows)
{
      string lyrsct = drow1["lyricist"].ToString();
      string sngrs = drow1["singers"].ToString();

      foreach (DataRow drow in dtfilmsngtemp.Rows)
      {
         string lid = drow["lyric_id"].ToString();
         string fid = drow["film_id"].ToString();
      }
} 

Try this

protected void Page_Load(object sender, EventArgs e)
{
    // Check
    if (!IsPostBack)
    {
        // Variable
        DataTable dt1 = new DataTable();
        DataTable dt2 = new DataTable();
        DataRow[] dr1 = null;
        DataRow[] dr2 = null;
        string value1 = string.Empty;
        string value2 = string.Empty;

        dt1.Columns.Add("A");
        dt1.Columns.Add("B");

        dt2.Columns.Add("A");
        dt2.Columns.Add("B");

        // Add to DataTable
        for (int i = 0; i < 10; i++)
        {
            dt1.Rows.Add(i.ToString(), (i + 1).ToString());
            dt2.Rows.Add(i.ToString(), (i + 2).ToString());
        }

        // Find By Select Example i want to take dt1 Column A = 2 and dt2 Column A = 9
        dr1 = dt1.Select("A=2"); // Select statement >>> Column = Value
        dr2 = dt2.Select("A=9");

        // Check & Get B Value
        if (dr1 != null && dr1.Length == 1) value1 = dr1[0]["B"] + "";
        if (dr2 != null && dr2.Length == 1) value2 = dr2[0]["B"] + "";

        Response.Write(value1 + ":" + value2);


    }
}

I think you need an inner join operation. Can't you do it in the SQL query? DataTable doesn't support that, but you can do it with LINQ. However, note that it will take O(n^2) time and shouldn't be used for big tables:

var results = from table1 in dtsnglyric.AsEnumerable()
             join table2 in dtfilmsngtemp.AsEnumerable() on (int)table1["lyricist"] equals (int)table2["lyric_id"]
             select new
             {
                 lyricist= (int)table1["lyricist"],
                 lyric_id= (int)table2["lyric_id"],
                 film_id= (int)table2["film_id"],
                 singers = (int)table1["singers"]
             };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM