LINQ中的Func委托与Lambda表达式

MSDN对于Func<T, TResult>)的官方解释: 封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。

        protected void Page_Load(object sender, EventArgs e)

        {

            var db = new DataClasses3DataContext();

            var f = db.Table_1s.AsEnumerable();

            Func<int, int> addone = delegate(int s) { return s=s+1; };

            GridView1.DataSource = f.Select(t=>t.id);

            GridView1.DataBind();

        }

运行结果

Item
1
2
3
4
5
6
7
8
9
        protected void Page_Load(object sender, EventArgs e)

        {

            var db = new DataClasses3DataContext();

            var f = db.Table_1s.AsEnumerable();

            Func<int, int> addone = delegate(int s) { return s=s+1; };

            GridView1.DataSource = f.Select(t=>addone(t.id));

            GridView1.DataBind();

        }

运行结果

Item
2
3
4
5
6
7
8
9
10

 

第2个例子 t=>addone(t.id)和t=>t.id+1 效果是一样的。

        protected void Page_Load(object sender, EventArgs e)

        {

            var db = new DataClasses3DataContext();

            var f = db.Table_1s.AsEnumerable();

            Func<int, int> addone = delegate(int s) { return s=s+1; };

            Func<int, bool> dayu = delegate(int s) { return s>3; };

            GridView1.DataSource = f.Where(t => dayu(t.id)).Select(t =>t.id);

            GridView1.DataBind();

        }

运行结果

Item
4
5
6
7
8
9

你可能感兴趣的:(lambda)