学习用,Linq中的Lambda语法和Linq查询语法比较,欢迎大家来看,嘿嘿,每个人心中也有自己的想法。
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Web;
 5using System.Web.UI;
 6using System.Web.UI.WebControls;
 7using System.Linq.Expressions;
 8using System.ComponentModel;
 9using System.Collections;
10
11public partial class Test : System.Web.UI.Page
12{
13
14    protected void Page_Load(object sender, EventArgs e)
15    {
16        List<test> TestList = new List<test>();
17        List<test_type> Test_TypeList = new List<test_type>();
18
19        for (int i = 100, j=0; i > 0; i--)
20        {
21            test_type tt = new test_type();
22            tt.id = i;
23            tt.name = "hello " + j;
24            Test_TypeList.Add(tt);
25            j++;
26        }

27        for (int i = 0; i < 10; i++)
28        {
29            test t = new test();
30            t.createdatetime = DateTime.Now;
31            t.id = i;
32            t.name = Guid.NewGuid().ToString() + i.ToString();
33            t.sex = (i % 2 == 0);
34            t.test_type_id = i;
35            TestList.Add(t);
36        }

37        
38        var ts = TestList.Join(Test_TypeList, t => t.test_type_id, tt => tt.id, (t, tt) => new { T = t, TT = tt })
39            .Where(t => t.T.name.Contains("a"))
40            .OrderByDescending(t => t.T.id)
41           .Select(t => new { t.T.id, t.T.name, t.T.createdatetime, ttypeName = t.TT.name });
42        
43        
44        var ts1 = from t in TestList
45                  join tt in Test_TypeList on t.test_type_id equals tt.id
46                  where t.name.Contains("a")
47                  orderby t.id descending
48                  select new { t.id, t.name, t.createdatetime, ttypeName =  tt.name};
49
50        foreach (var t in ts)
51        {
52            Response.Write(t.id + " " + t.name + " " + t.createdatetime + t.ttypeName + "<br />");
53        }

54        Response.Write("**************************************************************<br />");
55        foreach (var t in ts1)
56        {
57            Response.Write(t.id + " " + t.name + " " + t.createdatetime + t.ttypeName + "<br />");
58        }

59    }

60    public struct test
61    {
62        public int id;
63        public string name;
64        public DateTime createdatetime;
65        public bool sex;
66        public int test_type_id;
67    }

68    public struct test_type
69    {
70        public int id;
71        public string name;
72    }

73}

74
posted on 2009-09-15 23:53  凯龙  阅读(483)  评论(0编辑  收藏  举报