Home > Net >  Questions about entrusted as method parameters
Questions about entrusted as method parameters

Time:05-14

Where, for example, public static IEnumerable Where (this IEnumerable The source, Func Predicate);

The following example:
The static void Main (string [] args)
{
Int [] nums=new int [] {10, 20, 30, 40, 50};
Int sum0=nums. Sum ();
Console. WriteLine (" the sum of all: {0} ", sum0);//all of the data and
Int sum1=nums. Where (n=& gt; N % 4==0). The Sum ();
Console. WriteLine (" the sum of all: {0} ", sum1);//all multiples of the data and 4
Int sum2=nums. Where ((n, I)=& gt; I % 2==0). The Sum ();//10 + 30 + 50
Console. WriteLine (" the sum of all: {0} ", sum2);//all the subscript 4 multiples of the data and
}


Delegate invocation of the method is only a line, using the Lambda method, if there are a lot of lines of code in the delegate invocation method, what should I do?

CodePudding user response:

Well, understand the Func Good, what he means is introduced to a Tsouce object, return a bool

So no matter how many lines of code you have so he

So we can be written as

P=& gt; {

Bool res=false;

//line is omitted 1000 w

Retrun res



}

CodePudding user response:

Example
 
//a line written
Var q=myList. Where (x=& gt; X.I D==1);

//multiple lines written
Var q1=myList. Where (x=& gt;
{
If (x.I D==1)
return true;
The else
return false;
});
The Console. ReadKey ();

CodePudding user response:

Above I have example is LINQ to Object, in view of the generic collections for each of the generic Object to filter, if it is LINQ to the Dataset, the incoming is a set of the transformed data Object, each line is an Object? And use may be field object, the code method is also different,

For example: var result=the from b in ds. Tables [r]. "tb_Bookinfo AsEnumerable () where b. ield (" b_pub_date "). The Month==3
Aelect new
{
.

}

Not will be commissioned as a method of parameter, the where Predicate the heel?
  •  Tags:  
  • LINQ
  • Related