Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lambda表达式-如何在C#中处理委托?_C#_Delegates_Lambda - Fatal编程技术网

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

Lambda表达式-如何在C#中处理委托?,c#,delegates,lambda,C#,Delegates,Lambda,我从Jrista对一篇文章的回答中选取了以下示例 查找21个计数 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”委托,如何获得计数 我试着(请原谅我的语法错误) var otherway=Func numbers=x=>x==21; 您现在使用的匿名代理

我从Jrista对一篇文章的回答中选取了以下示例

查找21个计数

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”委托,如何获得计数

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

var otherway=Func numbers=x=>x==21;

您现在使用的匿名代理的签名与
Func
相同。如果您想改用方法,可以编写:

// a method that takes int and returns bool is
// a Func<int, bool> delegate
public bool IsEqualTo21(int x)
{ 
    return (x == 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;
}
Func method=MyMethod
int[]number=新[]{1,3,11,21,9,23,7,4,18,7,7,3,21};
整数计数=方法(21,数字);
私有布尔MyMethod(int numberToCheck,int[]数字)
{
整数计数=0;
foreach(数值中的变量编号)
{
if(number==numberToCheck)
{
计数++;
}
}
返回计数;
}
还有这个

变量数=新[]{1,3,11,21,9,23,7,4,18,7,7,3,21};
Count(i=>(i==21))

好的,如果您使用的是委托,那么我建议您坚持使用LINQ来进行实际的迭代,并且只在委托中实现测试。这是语法的C#2.0形式--“n=>n==21”样式仅仅是您所写内容的语法糖。
var twentyoneCount = numbers.Where(IsEqualTo21).Count();
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;
}
 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);
 });
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);
    }
}