linq 联合查询(Join)

查询人员信息

条件:当此人员信息在部门部门下面则查询

 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     }

 

你可能感兴趣的:(LINQ)