[C#] 纯文本查看 复制代码
using System;
using System.Linq;
using System.Collections.Generic;
namespace LinqExample
{
class Program
{
private static Random rd = new Random();
static void Main(string[] args)
{
OrderByExample();
}
public static void WhereExample()
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba", "Fatimah" };
foreach (var b in names.Where(a => a.StartsWith('S')))
{
Console.WriteLine(b);
}
}
public static void SelectExample()
{
List<SelectStruct> ls = new List<SelectStruct>();
for (int i = 0; i < 2000000; i++)
{
ls.Add(new SelectStruct(GetRdName(),i));
}
foreach (var item in
ls.
Where(c => c.name.StartsWith('g')).
Where(d => d.name.EndsWith('A')).
Where(f => f.name.Contains('f')).
Where(f => f.name.Contains('[')).
Select(a => a.name + " " + a.id.ToString())
)
{
Console.WriteLine(item);
}
//10进制 - 65~122
}
public static void AllAnyExample()
{
List<SelectStruct> ls = new List<SelectStruct>();
for (int i = 0; i < 20000; i++)
{
ls.Add(new SelectStruct(GetRdName().Substring(1,2), i));
}
Console.WriteLine(ls.All(d => d.name == "xY"));
Console.WriteLine(ls.Any(d => d.name == "xY"));
}
[Obsolete("此版本不支持此方法",true)]
public static void ChunkExample()
{
//Chunk
//string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba", "Fatimah" };
//foreach(var i in names.Chunk)
/*
适用于
产品 版本
.NET 6, 7, 8
*/
}
public static void AverageExample()
{
List<int> ls = new List<int>(){ 1, 2, 3, 4, 5, 5, 3, 56, 3, 3, 35, 32 };
Console.WriteLine(ls.Average());
}
public static void LastOrDefaultExample()
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba", "Fatimah" };
Console.WriteLine(names.LastOrDefault(x => x.StartsWith("S")));
Console.WriteLine(names.FirstOrDefault(x => x.StartsWith("S"))); ;
}
public static void UpperLowerCaseTest()
{
string[] list = {"smith","Student","stupid"}; //区分大小写
Console.WriteLine(list.FirstOrDefault(x => x.StartsWith("S")));
}
public static void OrderByDescendingExample()
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba", "Fatimah" };
foreach(var i in names.OrderByDescending(x => x, new CompareByLen()))
{
Console.WriteLine(i);
}
}
public static void OrderByExample()
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba", "Fatimah" };
foreach (var i in names.OrderBy(x => 9999 - x.Length)) //使用x.lenth,会自动分配到每个变量,然后进行升序排列 ---- 用99999 - lenth,就是降序排列
{
Console.WriteLine(i);
}
}
//************************************************华(hua)丽(ji)的分割线************************************************
private static string GetRdName()
{
string name = "";
for (int i = 0; i < 5; i++)
{
name += (char)rd.Next(65, 122);
}
return name;
}
private struct SelectStruct
{
public SelectStruct(string name, int id)
{
this.name = name;
this.id = id;
}
public string name;
public int id;
}
}
public class CompareByLen : IComparer<string>
{
public int Compare(string x, string y)
{
return x.Length - y.Length;
//CompareTo 方法返回一个整数,表示两个值的相对顺序。
//如果第一个值小于第二个值,则返回一个负数;
//如果第一个值等于第二个值,则返回零;
//如果第一个值大于第二个值,则返回一个正数。
}
}
}