Lambda expression - How to work on delegates in C#?
I took the following example from Jrista's answer to a post.
Finding Twentyone count
int[] numbers = new[] { 1, 3, 11, 21, 9, 23, 7, 4, 18, 7, 7, 开发者_JAVA技巧3, 21 };
var twentyoneCount = numbers.Where(n => n == 21).Count();
Suppose i use "Func" delegate how can i get the count ?
I tried as (pardon me for the bad syntax)
var otherway= Func <int> numbers= x => x==21;
You are using an anonymous delegate with the same signature as Func<int, bool>
right now. If you wanted to use a method instead, you could write:
// a method that takes int and returns bool is
// a Func<int, bool> delegate
public bool IsEqualTo21(int x)
{
return (x == 21);
}
And then use it as:
var twentyoneCount = numbers.Where(IsEqualTo21).Count();
int[] numbers = new[] { 1, 3, 11, 21, 9, 23, 7, 4, 18, 7, 7, 3, 21 };
int twentyoneCount = numbers.Count(delegate(int i)
{
return (i == 21);
});
Maybe something like:
Func<bool, int, int[]> method = MyMethod
int[] numbers = new[] { 1, 3, 11, 21, 9, 23, 7, 4, 18, 7, 7, 3, 21 };
int count = method(21, numbers);
private bool MyMethod(int numberToCheck, int[] numbers)
{
int count = 0;
foreach (var number in numbers)
{
if (number == numberToCheck)
{
count++;
}
}
return count;
}
Also this
var numbers = new[] { 1, 3, 11, 21, 9, 23, 7, 4, 18, 7, 7, 3, 21 }; numbers.Count(i => (i == 21));
protected void btnSave_Click(object sender, EventArgs e)
{
if (ddlClient.SelectedItem.Text != "Select")
{
//Page.Validate("AdvancePayment");
//Page.Validate("Project");
//Page.Validate("group3");
if (Page.IsValid)
{
bool bolSuccess = false;
SqlTransaction SqlTrn;
SqlConnection con = new SqlConnection(CommonStrings.ConnectionString);
con.Open();
SqlTrn = con.BeginTransaction();
try
{
bool bolUpdate;
int intParameterLength;
bolUpdate = ((ViewState["Project_ID"] == null) ? false : true);
if (bolUpdate == true)
{
intParameterLength = 2;
}
else
{
intParameterLength = 1;
}
object objProjectCost;
objProjectCost = CommonFunctions.GetDbNull(txtProjectCost.Text);
ExecuteProcedures ex = new ExecuteProcedures(intParameterLength);
ex.Parameters.Add("@Client_ID", SqlDbType.Int, ddlClient.SelectedValue);
//ex.Parameters.Add("@vcrProject", SqlDbType.VarChar, ddlProject.SelectedItem.Text);
//ex.Parameters.Add("@numRate", SqlDbType.Float, Convert.ToDouble(txtProjectCost.Text));
//ex.Parameters.Add("@vcrShort_Description", SqlDbType.VarChar, txtShortDesc.Text);
//ex.Parameters.Add("@numAdvance_Amount", SqlDbType.Float, Convert.ToDouble(txtAdvanceAmount.Text));
//ex.Parameters.Add("@Date", SqlDbType.DateTime, Convert.ToDateTime(txtAdvanceDate.Text));
//ex.Parameters.Add("@PaymentDetails", SqlDbType.VarChar, txtPayment.Text);
string strProcedure = "proc_Add_Client_Order";
if (bolUpdate == true)
{
strProcedure = "proc_Update_Client_Order_new";
ex.Parameters.Add("@Client_Project_ID", SqlDbType.VarChar, Convert.ToString(ViewState["Project_ID"]));
}
string ID = Convert.ToString(ex.InvokeProcedure(strProcedure, SqlTrn, ValueDataType.Number, con));
if (bolUpdate == true)
{
ID = ViewState["Project_ID"].ToString();
}
if (GridView1.Rows.Count > -1)
{
Dentry de = new Dentry();
string strSql = "Delete from Client_Order_Advance_Payment where Client_Order_ID=" + ID;
de.RunCommand(strSql, SqlTrn, con);
foreach (GridViewRow grdRow in GridView1.Rows)
{
Label lbld = (Label)grdRow.FindControl("lblPayment_Date");
string strDate = lbld.Text;
string strAmount = grdRow.Cells[1].Text;
//string strDate = ((Label)grdRow.FindControl("lblPayment_Date")).Text;
//string strExpectedDate = grdRow.Cells[4].Text;
Label lbled = (Label)grdRow.FindControl("lblExpectedPayment_Date");
string strExpectedDate = lbled.Text;
//string strExpectedDate = ((Label)grdRow.FindControl("lblExpectedPayment_Date")).Text;
string strPaymentDetails = grdRow.Cells[0].Text;
string ExpectedTime = grdRow.Cells[5].Text;
string DispatchedTime = grdRow.Cells[3].Text;
strSql = "Insert into Client_Order_Advance_Payment(numAdvance_Amount,dtPayment_Date,Client_Order_ID,";
strSql += "vcrPayment_Details,dtExpectedPayment_Date,ExpTime,Dispath_Time,Enquiry_ID) values(" + strAmount +
",'" + Convert.ToDateTime(strDate) + "'," + ID + ",'" + strPaymentDetails + "','" +
Convert.ToDateTime(strExpectedDate) + "','" + ExpectedTime + "','" +
DispatchedTime + "'," + ID + ")";
de.RunCommand(strSql, SqlTrn, con);
}
}
if (GridView2.Rows.Count > -1)
{
Dentry de = new Dentry();
string strSql = "Delete from Client_Ordered_Projects where Client_Order_ID=" + ID;
de.RunCommand(strSql, SqlTrn, con);
foreach (GridViewRow grdRow in GridView2.Rows)
{
string strProject = grdRow.Cells[0].Text;
string strRate = grdRow.Cells[1].Text;
string strDescription = grdRow.Cells[2].Text;
strSql = "Insert into Client_Ordered_Projects(vcrProject,numRate,vcrDescription,Client_Order_ID,Enquiry_ID" +
") values('" + strProject + "'," + strRate + ",'" + strDescription + "'," + ID + "," + ID + ")";
de.RunCommand(strSql, SqlTrn, con);
}
}
SqlTrn.Commit();
bolSuccess = true;
}
catch
{
SqlTrn.Rollback();
}
con.Close();
if (bolSuccess == true)
{
Response.Redirect("Client_Order_Grid.aspx");
}
}
}
else
{
CommonFunctions.Alert("Plz. Select the Client",this.Page);
}
}
精彩评论