using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
namespace LinqQuery
{
class Program
{
static void Main()
{
int[] array = { 1, 2, 3, 6, 7, 8 };
// Query expression.
var elements = from element in array
orderby element descending
where element > 2
select element;
// Enumerate.
foreach (var element in elements)
{
Console.Write(element);
Console.Write(' ');
}
Console.WriteLine();
Program2.Main2();
Program3.Main3();
Program4.Main4();
Program5.Main5();
Program6.Main6();
Program7.Main7();
}
}
/*
8 7 6 3
请按任意键继续. . .
*/
class Program2
{
const int _max = 1000000;
public static void Main2()
{
int[] values = { 10, 0, 1, 1, 20, 300, 400, 4 };
// Version 1: use LINQ.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
int count = CountLinq(values);
}
s1.Stop();
// Version 2: use for-loop.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
int count = CountFor(values);
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)12.345).ToString("0.00 ns"));//12.35 ns
Console.Read();
}
static int CountLinq(int[] values)
{
// Count values greater than or equal to 10 with LINQ.
return (from x in values
where x >= 10
select x).Count();
}
static int CountFor(int[] values)
{
// Count values greater than or equal to 10 with a loop.
int count = 0;
for (int i = 0; i < values.Length; i++)
{
if (values[i] >= 10)
{
count++;
}
}
return count;
}
/*
120.38 ns
33.32 ns
*/
}
class Program3
{
public static void Main3()
{
int[] array1 = { 1, -1, -2, 0 };
// Find maximum number.
Console.WriteLine(array1.Max());
// Find maximum number when all numbers are made positive.
Console.WriteLine(array1.Max(element => Math.Abs(element)));
}
/*
1
2
请按任意键继续. . .
*/
}
class Program4
{
public static void Main4()
{
int[] array1 = { 1, -1, 2, 0 };
// Find minimum number.
Console.WriteLine(array1.Min());
// Find minimum number when all numbers are made negative.
Console.WriteLine(array1.Min(element => -element));
}
/*
-1
-2
请按任意键继续. . .
*/
}
class Program5
{
public static void Main5()
{
List<int> elements = new List<int>() { 10,33, 20, 31, 40 };
// ... Find index of first odd element.
int oddIndex = elements.FindIndex(x => x % 2 != 0);
Console.WriteLine(oddIndex);//1
Console.WriteLine(elements[oddIndex]);//33
}
}
class Program6
{
public static void Main6()
{
//
// Use implicitly typed lambda expression.
// ... Assign it to a Func instance.
//
Func<int, int> func1 = new Func<int,int>( x => x + 1);
//
// Use lambda expression with statement body.
//
Func<int, int> func2 = x => { return x + 1; };
//
// Use formal parameters with expression body.
//
Func<int, int> func3 = (int x) => x + 1;
//
// Use parameters with a statement body.
//
Func<int, int> func4 = (int x) => { return x + 1; };
//
// Use multiple parameters.
//
Func<int, int, int> func5 =new Func<int,int,int>( (x, y) => x * y );
//
// Use no parameters in a lambda expression.
//
Action func6 = new Action( () => Console.WriteLine("----------") );
//
// Use delegate method expression.
//
Func<int, int> func7 = delegate(int x) { return x + 1; };
//
// Use delegate expression with no parameter list.
//
Func<int> func8 = delegate { return 1 + 1; };
//
Func<int, int, int> func9 = delegate(int x, int y) { return x * y; };
// Invoke each of the lambda expressions and delegates we created.
// ... The methods above are executed.
//
Console.WriteLine(func1.Invoke(1));
Console.WriteLine(func2.Invoke(1));
Console.WriteLine(func3.Invoke(1));
Console.WriteLine(func4.Invoke(1));
Console.WriteLine(func5.Invoke(2, 2));
func6.Invoke();
Console.WriteLine(func7.Invoke(1));
Console.WriteLine(func8.Invoke());
Console.WriteLine(func9.Invoke(2, 3));
/*
2
2
2
2
4
2
2
请按任意键继续. . .
*/
}
}
class Program7
{
static int[] array = { 1,2,1,3,1 };
static Func<int, bool> f = delegate(int x)
{
return x == 1;
};
public static void Main7()
{
Predicate<int> predicate = value => value == 5;
Console.WriteLine(predicate.Invoke(4));
Console.WriteLine(predicate.Invoke(5));
//Lambda expression tested: C#
int c = array.Count(element => element == 1);
Console.WriteLine(c);
//Delegate tested: C#
c = array.Count(f);
Console.WriteLine(c);//3
}
/*
False
True
请按任意键继续. . .
*/
}
}
LinqQuery
最新推荐文章于 2023-11-21 22:48:11 发布