linq連合クエリー(Join)

9895 ワード

ユーザ情報の照会
条件:この担当者情報が部門の下にある場合に照会します.
 1  public partial class LinqJoin : System.Web.UI.Page

 2     {

 3        

 4         protected void Page_Load(object sender, EventArgs e)

 5         {

 6             DataLoad();

 7             DataInit();

 8         }

 9         /// <summary>

10         /// Sql 

11         /// </summary>

12         public void DataInit()

13         {

14             // 

15             Stopwatch watch = new Stopwatch();

16             // 

17             watch.Start();

18             using (SqlConnection conn = new SqlConnection(@"server=HAPPY-PC;User ID=sa;Password=happy;database=Exam4.1_OK;Connection Reset=FALSE;Max Pool Size = 512;"))

19             {

20                 conn.Open();

21 

22                 using (SqlCommand cmd = conn.CreateCommand())

23                 {

24                     // UserId<276234 Id 

25                     cmd.CommandText = "select a.* from users a inner join Structure c on a.StructureID=c.StructureID where a.UserId<276234";

26 

27                     SqlDataAdapter adapter = new SqlDataAdapter();

28                     adapter.SelectCommand = cmd;

29                     DataSet ds = new DataSet();

30 

31                     adapter.Fill(ds);

32 

33                     this.GridView1.DataSource = ds;

34                     this.GridView1.DataBind();

35                     ds.Dispose();

36 

37                 }

38             }

39             // 

40             watch.Stop();

41             // 

42             Response.Write(""+watch.Elapsed.Milliseconds.ToString());

43         }

44         /// <summary>

45         /// Linq 

46         /// </summary>

47         private  void DataLoad()

48         {

49             // 

50             Stopwatch watch = new Stopwatch();

51             // 

52             watch.Start();

53             using (LinqInnerJoinDataContext ctx = new LinqInnerJoinDataContext("server=HAPPY-PC;User ID=sa;Password=happy;database=Exam4.1_OK;Connection Reset=FALSE;Max Pool Size = 512;"))

54             {

55                 // UserId<276234 Id 

56                 var table = from c in ctx.Users

57                             join p in ctx.Structure

58                             on c.StructureID equals p.StructureID.ToString()

59                             where c.UserID < 276234

60                             select c;

61 

62                 this.GridView2.DataSource = table;

63                 this.GridView2.DataBind();

64             }

65             // 

66             watch.Stop();

67             // 

68             Response.Write(" :" + watch.Elapsed.Milliseconds.ToString());

69         }

70     }