2009-10-12 6 views
0

나는 Jrista의 답변에서 다음의 예를 취했다.람다 식 - C#에서 대리인 작업하는 방법?

찾기 Twentyone은

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"대리인 내가 어떻게이 수를 얻을 수 있습니다 사용한다고 가정 해 계산?

내가 지금 Func<int, bool>과 동일한 서명을 익명 대리자를 사용하여

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

답변

5

당신 있습니다을 (나쁜 구문 실례)로 시도 .

// 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(); 
1

아마 같은 : 또한

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

위임자를 사용하는 경우 LINQ를 사용하여 실제 반복을 수행하고 위임자 만 테스트를 구현하는 것이 좋습니다. – Groo

2
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); 
}); 
+0

이것은 C# 2.0 형식의 구문입니다. "n => n == 21"스타일은 단순히 사용자가 작성한 구문에 대한 설탕입니다. –

0

var numbers = new [] {1, 3, 11, 21, 9, 23, 7, 4, 18, 7, 7, 3, 21}; numbers.Count (i => (i == 21));

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