• Linq扩展方法CountBy和AggregateBy的用法
  • 发布于 1个月前
  • 68 热度
    0 评论
Json缩进
增加了Json输出时的缩进控制,代码如下:
using System.Text.Json;
var options = new JsonSerializerOptions
{
    //堆代码 duidaima.com
    //启用缩进
    WriteIndented = true,
    //缩进字符
    IndentCharacter = ' ',
    //缩进大小
    IndentSize = 6,

    Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
string json = JsonSerializer.Serialize(
    new { Name = "张三",Age=20,Sex="男" },
    options
    );
Console.WriteLine(json);
需要注意支持的缩进字符只有空格和水平制表符(tab键),其他字符会报错。下面是输出结果。
{
      "Name": "张三",
      "Age": 20,
      "Sex": "男"
}
JsonSerializerOptions原来提供了一个JsonSerializerOptions.Defalut的配置同时提供了一个,现在又加了一个JsonSerializerOptions.Web,默认为驼峰命名策略。
var webJson = JsonSerializer.Serialize(
    new { UserName = "gsw", Password="123456" },
    JsonSerializerOptions.Web // Defaults to camelCase naming policy.
    );
Console.WriteLine(webJson);
结果:
{"userName":"gsw","password":"123456"}

Linq扩展方法CountBy和AggregateBy
CountBy,把key相同的进行相加,返回一个KeyValue集合。MaxBy和MinBy获取整个集合中最多和最少的KeyValue,如果有相同的,取第一个。
var sourceText = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed non risus. Suspendisse lectus tortor, dignissim sit amet, 
adipiscing nec, ultricies sed, dolor. Cras elementum ultrices amet diam.
""";
Console.WriteLine(sourceText);

var KVs = sourceText
    .Split(new char[] { ' ', '.', ',','\r','\n' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(word => word.ToLowerInvariant())
    .CountBy(word => word);
foreach (var item in KVs)
{
    Console.WriteLine(item.Key + ":" + item.Value);
}
Console.WriteLine("-------------------------");
var mostFrequentWord = KVs.MaxBy(pair => pair.Value);
Console.WriteLine(mostFrequentWord.Key + ":" + mostFrequentWord.Value);
Console.WriteLine("-------------------------");
mostFrequentWord = KVs.MinBy(pair => pair.Value);
Console.WriteLine(mostFrequentWord.Key + ":" + mostFrequentWord.Value);
运行结果:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed non risus. Suspendisse lectus tortor, dignissim sit amet,
adipiscing nec, ultricies sed, dolor. Cras elementum ultrices amet diam.
lorem:1
ipsum:1
dolor:2
sit:2
amet:3
consectetur:1
adipiscing:2
elit:1
sed:2
non:1
risus:1
suspendisse:1
lectus:1
tortor:1
dignissim:1
nec:1
ultricies:1
cras:1
elementum:1
ultrices:1
diam:1
-------------------------
amet:3
-------------------------
lorem:1
AggregateBy是对KeyValue集合进行求和计算。
var orders = new Order[] {
       new Order("zs", 1,DateTime.Now),
       new Order("ls", 2,DateTime.Now),
       new Order("ww", 3,DateTime.Now),
       new Order("zs", 4,DateTime.Now),
       new Order("ls", 5,DateTime.Now),
       new Order("ww", 6,DateTime.Now),
    };
var kvs = orders.AggregateBy(
     keySelector: word => word.OrderUser,
        seed: 0m,
        (orderAmount, order) => orderAmount + order.OrderAmount
        );
foreach (var item in kvs)
{
    Console.WriteLine(item.Key + ":" + item.Value);
}

record Order(string OrderUser, decimal OrderAmount, DateTime OrderTime);
运行结果:
zs:5
ls:7
ww:9

用户评论