C# LINQ Inner Join DataTable ( Int32 == Int16 ) 方法

 
C# LINQ Inner Join DataTable ( Int32 == Int16 ) 方法
 
Add Reference : System.Data.DataSetExtensions
 

 
   using System;
   using System.Linq;
   using System.Data;
 
   namespace WebApplication13
   {
      public partial class WebForm5 : System.Web.UI.Page
      {
         protected void Page_Load(object sender, EventArgs e)
         {
            try
            {
               DataTable Player_dt = GetPlayer();
               DataTable Team_dt = GetTeam();
 
               var results =
               from table1 in Player_dt.AsEnumerable()
               join table2 in Team_dt.AsEnumerable()
               on table1["Team_ID"].ToString() equals table2["Team_ID"].ToString()
               select new
               {
                  Team_Name = (String)table2["Team_Name"],
                  Player_Name = (String)table1["Player_Name"],
               };
 
               this.GridView1.DataSource = results;
               this.GridView1.DataBind();
            } catch (Exception ex) {
               Response.Write(ex.Message);
            }
         }
 
         private DataTable GetPlayer()
         {
            … …
         }
 
         private DataTable GetTeam()
         {
            … …
         }
      }
   }