繁体   English   中英

Lambda表达式-如何在C#中处理委托?

[英]Lambda expression - How to work on delegates in C#?

我从Jrista的帖子中得到了以下示例。

查找二十一计数

int[] numbers = new[] { 1, 3, 11, 21, 9, 23, 7, 4, 18, 7, 7, 3, 21 };

var twentyoneCount = numbers.Where(n => n == 21).Count();

我尝试了(请原谅我的语法错误)

  

正在使用具有与Func<int, bool>相同签名的匿名委托。 如果您想使用一种方法,则可以编写:

// a method that takes int and returns bool is
// a Func<int, bool> delegate
public bool IsEqualTo21(int x)
{ 
    return (x == 21);
}

然后将其用作:

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);
 });

也许像这样:

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;
}

还有这个

var number = new [] {1,3,11,21,9,23,7,4,4,18,7,7,3,21}; number.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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM