Aggregate Function:
To get a comma separated list of countries.
string[] Countries = {“India”, “US”, “UK”,
“Canada”,” Australia”};
1.
Without Linq
string
result = string.empty;
for(int
i=0; I < Countries.Length; i++)
{
result = result + Countries[i] + “, ” ;
}
int lastIndex = result. LastIndexOf(“, ”);
result = result.Remove(lastIndex)
2.
With Linq
string
result = Countries.Aggregate( (a,b) => a + “, “ + b);
How Aggregate() function
works?
Step 1. First "India" is concatenated with "US" to produce result "India, US"
Step 2. Result in Step 1 is then concatenated with "UK" to produce result "India, US, UK"
Step 3: Result in Step 2 is then concatenated with "Canada" to produce result "India, US, UK, Canada"
This goes on until the last element in the array to produce the final single string "India, US, UK, Canada, Australia"
Step 1. First "India" is concatenated with "US" to produce result "India, US"
Step 2. Result in Step 1 is then concatenated with "UK" to produce result "India, US, UK"
Step 3: Result in Step 2 is then concatenated with "Canada" to produce result "India, US, UK, Canada"
This goes on until the last element in the array to produce the final single string "India, US, UK, Canada, Australia"
Similarly, To compute the product of all
numbers in an Int Array.
int[] numbers = {2,3,4,5 };
int result = numbers.Aggregate( (a,b) =>
a*b);
We can also provide a seed parameter in
Aggregate Function.
int result = numbers.Aggregate(10, (a,b)
=> a*b);
//then
result will be:1200 bcoz ((((10*2)*3)*4)*5)
// this seed value will be multiplied by
every value of the array.
Step 1: Multiply (10X2) to produce result 20
Step 2: Result (20) in Step 1 is then multiplied with 3 (20X3) to produce result 60
Step 3: Result (60) in Step 2 is then multiplied with 4 (60X4) to produce result 240
Step 4: Result (240) in Step 3 is then multiplied with 5 (240X5) to produce final result 1200
Step 2: Result (20) in Step 1 is then multiplied with 3 (20X3) to produce result 60
Step 3: Result (60) in Step 2 is then multiplied with 4 (60X4) to produce result 240
Step 4: Result (240) in Step 3 is then multiplied with 5 (240X5) to produce final result 1200
List<int> numbers = new List<int> {1,2,3,….10};
Func<int,bool> mypredicate = x=> x%2 == 0;
IEnumerable<int> evenNum =
numbers.Where(mypredicate);
or
we can create a function which
returns a Boolean and replace the
mypredicate above.
Private static bool IsEven (int
number)
{
|
{
return true;
}
else{
return false;
}
//or simply use :
//return number % 2 == 0;
}
So, now we can use
IEnumerable<int> evenNums =
numbers.Where(x => IsEven(x));
or
IEnumerable<int> evenNums = from a in
numbers
Where
a % 2 ==0
Select a;
To Print the numbers and their indexes.
List<int> numbers = new List<int>
{1,2,3,4,……10};
var result = numbers.Select( (num,idx) =>
new {TheNumber = num, TheIDX = idx});
foreach(var item in result)
{
C.W
(item.TheNumber + “ ---
“ + item.TheIDX )
}
And to find the Numbers & Indexes of all
even numbers.
var result = numbers
.Select( (num,idx) => new {TheNumber = num,
Theidx = idx})
.Where (x=> x.TheNumber % 2 ==0);
And to find only the indexes of Even numbers.
var result = numbers
.Select( (num,idx) => new {TheNumber = num,
Theidx = idx})
.Where (x=>
x.TheNumber % 2 ==0);
.Select(x=> x.TheIdx) ;
Linq Standard Query operators, also called as
Linq Extension methods, can be broadly classified into following categories
ie. Aggregate Operators, Generation,
Grouping, Query Execution, Restriction, Join, Projection, Custom Sequence, Set,
Quantifiers, Partitioning, Conversion, Miscellaneous Operators.
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
int smallestNum = numbers.Min();
int smallestEvenNum = numbers.where(n=>
n%2 ==0).Min();
int largestNum = numbers.Max();
int largestEvenNum = numbers.where(n=> n%2
==0).Max();
int sumOfAllNums = numbers.Sum();
int sumOfAllEvenNums = numbers.where(n=>
n%2 ==0).Sum();
int countOfAllNums = numbers.Count();
int countOfAllEvenNums = numbers.where(n=>
n%2 ==0).Count();
double averageOfAllNums = numbers.Average();
double averageOfAllEvenNums =
numbers.where(n=> n%2 ==0).Average();
string[] Countries = {“India”, “USA”, “UK”,
“Canada”};
int minCount = Countries.Min(x=>
x.Length);
int maxCount = Countries.Max(x=>
x.Length);
C.W(“Shortest country name has {0}
characters”, minCount);
C.W(“Longest country name has {0} characters”, maxCount);
0 comments:
Post a Comment