MSDN official explanation for Func <T, TResult>): encapsulate a method that has a parameter and returns the type value specified by the TResult parameter.
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(); }
Running result
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(); }
Running result
In the first example, t => addone (t. id) and t => t. id + 1 have the same effect.
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(); }
Running result