1

How can i declare variables in below method

For example:

double price = double.Parse(((TextBox)r.FindControl("txtUnitprice")).Text); 

double total = GvProducts.Rows
                         .Cast<GridViewRow>()
                         .Where(r => ((CheckBox)r.FindControl("chkSel")).Checked)
                         .Sum(r => 
                              double.Parse(((TextBox)r.FindControl("txtQuantity")).Text) * 
                              double.Parse(((TextBox)r.FindControl("txtUnitprice")).Text));
Rob
  • 26,989
  • 16
  • 82
  • 98
Ayman
  • 842
  • 6
  • 16
  • 31
  • With query syntax you can use the `let` keyword, in method syntax you could use an anonymous type. – Tim Schmelter Jan 14 '16 at 12:46
  • `r => { double price = double.Parse(((TextBox)r.FindControl("txtUnitprice")).Text); /* do something with price */; return price; }`. Note that if you are passing an Expression (I believe in this case you are *not*), then you are unable to write an expression block (and thus cannot define a variable inside the expression) – Rob Jan 14 '16 at 12:46

1 Answers1

1

When any method expects a Func you have two possibilites (or to be exact three).

  1. Add a single-line-expression (as already done)
  2. Add the name of a delegate:

    Sum(x => MyMethod(x)) Where MyMethod is a method within your class returning an int and expecting a T).

  3. Add a multi-line expression embraced in curly brackets:

    Sum(x => { /* any statements */ })

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111