浏览代码

增加CompareChanges重载

懒得勤快 10 月之前
父节点
当前提交
a99de13dec

+ 55 - 19
Masuit.Tools.Abstractions/Extensions/BaseType/IEnumerableExtensions.cs

@@ -999,13 +999,51 @@ public static class IEnumerableExtensions
         return !enumerator2.MoveNext();
     }
 
+    /// <summary>
+    /// 对比两个集合哪些是新增的、删除的、修改的
+    /// </summary>
+    /// <param name="first">新集合</param>
+    /// <param name="second">旧集合</param>
+    /// <returns></returns>
+    public static (List<T> adds, List<T> remove, List<T> updates) CompareChanges<T>(this IEnumerable<T> first, IEnumerable<T> second)
+    {
+        first ??= new List<T>();
+        second ??= new List<T>();
+        var firstSource = first as ICollection<T> ?? first.ToList();
+        var secondSource = second as ICollection<T> ?? second.ToList();
+        var add = firstSource.Except(secondSource).ToList();
+        var remove = secondSource.Except(firstSource).ToList();
+        var update = firstSource.Intersect(secondSource).ToList();
+        return (add, remove, update);
+    }
+
+    /// <summary>
+    /// 对比两个集合哪些是新增的、删除的、修改的
+    /// </summary>
+    /// <typeparam name="TKey">对比因素</typeparam>
+    /// <param name="first">新集合</param>
+    /// <param name="second">旧集合</param>
+    /// <param name="keySelector">对比因素(可唯一确定元素的字段)</param>
+    /// <returns></returns>
+    public static (List<T> adds, List<T> remove, List<T> updates) CompareChanges<T, TKey>(this IEnumerable<T> first, IEnumerable<T> second, Func<T, TKey> keySelector)
+    {
+        first ??= new List<T>();
+        second ??= new List<T>();
+        var firstSource = first as ICollection<T> ?? first.ToList();
+        var secondSource = second as ICollection<T> ?? second.ToList();
+        var add = firstSource.ExceptBy(secondSource, keySelector).ToList();
+        var remove = secondSource.ExceptBy(firstSource, keySelector).ToList();
+        var update = firstSource.IntersectBy(secondSource, keySelector).ToList();
+        return (add, remove, update);
+    }
+
     /// <summary>
     /// 对比两个集合哪些是新增的、删除的、修改的
     /// </summary>
     /// <typeparam name="T1"></typeparam>
     /// <typeparam name="T2"></typeparam>
-    /// <param name="first"></param>
-    /// <param name="second"></param>
+    /// <param name="first">新集合</param>
+    /// <param name="second">旧集合</param>
     /// <param name="condition">对比因素条件</param>
     /// <returns></returns>
     public static (List<T1> adds, List<T2> remove, List<T1> updates) CompareChanges<T1, T2>(this IEnumerable<T1> first, IEnumerable<T2> second, Func<T1, T2, bool> condition)
@@ -1026,8 +1064,8 @@ public static class IEnumerableExtensions
     /// <typeparam name="T1">集合1</typeparam>
     /// <typeparam name="T2">集合2</typeparam>
     /// <typeparam name="TKey">对比因素</typeparam>
-    /// <param name="first">集合1</param>
-    /// <param name="second">集合2</param>
+    /// <param name="first">集合</param>
+    /// <param name="second">集合</param>
     /// <param name="firstKeySelector">集合1的对比因素(可唯一确定元素的字段)</param>
     /// <param name="secondKeySelector">集合2的对比因素(可唯一确定元素的字段)</param>
     /// <returns></returns>
@@ -1046,28 +1084,26 @@ public static class IEnumerableExtensions
     /// <summary>
     /// 对比两个集合哪些是新增的、删除的、修改的
     /// </summary>
-    /// <typeparam name="TKey">对比因素</typeparam>
-    /// <param name="first">集合1</param>
-    /// <param name="second">集合2</param>
-    /// <param name="keySelector">对比因素(可唯一确定元素的字段)</param>
+    /// <param name="first">新集合</param>
+    /// <param name="second">旧集合</param>
     /// <returns></returns>
-    public static (List<T> adds, List<T> remove, List<T> updates) CompareChanges<T, TKey>(this IEnumerable<T> first, IEnumerable<T> second, Func<T, TKey> keySelector)
+    public static (List<T> adds, List<T> remove, List<(T first, T second)> updates) CompareChangesPlus<T>(this IEnumerable<T> first, IEnumerable<T> second)
     {
         first ??= new List<T>();
         second ??= new List<T>();
         var firstSource = first as ICollection<T> ?? first.ToList();
         var secondSource = second as ICollection<T> ?? second.ToList();
-        var add = firstSource.ExceptBy(secondSource, keySelector).ToList();
-        var remove = secondSource.ExceptBy(firstSource, keySelector).ToList();
-        var update = firstSource.IntersectBy(secondSource, keySelector).ToList();
-        return (add, remove, update);
+        var add = firstSource.Except(secondSource).ToList();
+        var remove = secondSource.Except(firstSource).ToList();
+        var updates = firstSource.Intersect(secondSource).Select(t1 => (t1, secondSource.FirstOrDefault(t2 => t1.Equals(t2)))).ToList();
+        return (add, remove, updates);
     }
 
     /// <summary>
     /// 对比两个集合哪些是新增的、删除的、修改的
     /// </summary>
-    /// <param name="first"></param>
-    /// <param name="second"></param>
+    /// <param name="first">新集合</param>
+    /// <param name="second">旧集合</param>
     /// <param name="keySelector">对比因素</param>
     /// <returns></returns>
     public static (List<T> adds, List<T> remove, List<(T first, T second)> updates) CompareChangesPlus<T, TKey>(this IEnumerable<T> first, IEnumerable<T> second, Func<T, TKey> keySelector)
@@ -1087,8 +1123,8 @@ public static class IEnumerableExtensions
     /// </summary>
     /// <typeparam name="T1"></typeparam>
     /// <typeparam name="T2"></typeparam>
-    /// <param name="first"></param>
-    /// <param name="second"></param>
+    /// <param name="first">新集合</param>
+    /// <param name="second">旧集合</param>
     /// <param name="condition">对比因素条件</param>
     /// <returns></returns>
     public static (List<T1> adds, List<T2> remove, List<(T1 first, T2 second)> updates) CompareChangesPlus<T1, T2>(this IEnumerable<T1> first, IEnumerable<T2> second, Func<T1, T2, bool> condition)
@@ -1108,8 +1144,8 @@ public static class IEnumerableExtensions
     /// </summary>
     /// <typeparam name="T1"></typeparam>
     /// <typeparam name="T2"></typeparam>
-    /// <param name="first"></param>
-    /// <param name="second"></param>
+    /// <param name="first">新集合</param>
+    /// <param name="second">旧集合</param>
     /// <param name="firstKeySelector">集合1的对比因素(可唯一确定元素的字段)</param>
     /// <param name="secondKeySelector">集合2的对比因素(可唯一确定元素的字段)</param>
     /// <returns></returns>

+ 1 - 9
Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj

@@ -1,6 +1,6 @@
 <Project Sdk="Microsoft.NET.Sdk">
     <PropertyGroup>
-        <TargetFrameworks>netstandard2.0;netstandard2.1;net461;net6;net7;net8;net9</TargetFrameworks>
+        <TargetFrameworks>netstandard2.0;netstandard2.1;net461;net6;net8;net9</TargetFrameworks>
         <LangVersion>latest</LangVersion>
         <ImplicitUsings>enable</ImplicitUsings>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@@ -53,7 +53,6 @@
         <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
         <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
         <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
-        <!--<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />-->
         <PackageReference Include="System.Management" Version="9.0" />
         <PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
         <PackageReference Include="SharpCompress" Version="0.38.0" />
@@ -88,13 +87,6 @@
         <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4" />
     </ItemGroup>
 
-    <ItemGroup Condition=" '$(TargetFramework)' == 'net7'">
-        <PackageReference Include="Castle.Core" Version="5.1.1" />
-        <PackageReference Include="System.Diagnostics.PerformanceCounter" Version="7.0.0" />
-        <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
-        <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4" />
-    </ItemGroup>
-
     <ItemGroup Condition=" '$(TargetFramework)' == 'net8'">
         <PackageReference Include="Castle.Core" Version="5.1.1" />
         <PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.1" />

+ 2 - 5
Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
     <PropertyGroup>
-        <TargetFrameworks>netcoreapp3.1;net6;net7;net8;net9</TargetFrameworks>
+        <TargetFrameworks>netcoreapp3.1;net6;net8;net9</TargetFrameworks>
         <ImplicitUsings>enable</ImplicitUsings>
         <Description>
             全龄段友好的C#万能工具库,码数吐司库(适用于.NET Core Web项目),不管你是菜鸟新手还是骨灰级玩家都能轻松上手,包含一些常用的操作类,大都是静态类,加密解密,反射操作,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。包含一些AspNetCore常用的工具类、ModelBinder等。
@@ -44,7 +44,7 @@
 
     <ItemGroup>
         <FrameworkReference Include="Microsoft.AspNetCore.App" />
-        <PackageReference Include="FastExpressionCompiler" Version="4.2.2" />
+        <PackageReference Include="FastExpressionCompiler" Version="5.0.0" />
     </ItemGroup>
 
     <ItemGroup>
@@ -60,9 +60,6 @@
     <ItemGroup Condition=" '$(TargetFramework)' == 'net6'">
         <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.36" />
     </ItemGroup>
-    <ItemGroup Condition=" '$(TargetFramework)' == 'net7'">
-        <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.20" />
-    </ItemGroup>
     <ItemGroup Condition=" '$(TargetFramework)' == 'net8'">
         <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.11" />
     </ItemGroup>

+ 1 - 4
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
     <PropertyGroup>
-        <TargetFrameworks>netstandard2.0;netstandard2.1;net6;net7;net8;net9</TargetFrameworks>
+        <TargetFrameworks>netstandard2.0;netstandard2.1;net6;net8;net9</TargetFrameworks>
         <Description>全龄段友好的C#万能工具库,码数吐司库(适用于.NET Core项目),不管你是菜鸟新手还是骨灰级玩家都能轻松上手,包含一些常用的操作类,大都是静态类,加密解密,反射操作,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。
 官网教程:https://tools.masuit.org
 github:https://github.com/ldqk/Masuit.Tools
@@ -50,9 +50,6 @@ github:https://github.com/ldqk/Masuit.Tools
     <ItemGroup Condition=" '$(TargetFramework)' == 'net6'">
         <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.36" />
     </ItemGroup>
-    <ItemGroup Condition=" '$(TargetFramework)' == 'net7'">
-        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.20" />
-    </ItemGroup>
     <ItemGroup Condition=" '$(TargetFramework)' == 'net8'">
         <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.11" />
     </ItemGroup>

+ 1 - 1
Masuit.Tools.Excel/Masuit.Tools.Excel.csproj

@@ -37,7 +37,7 @@
       </None>
     </ItemGroup>
     <ItemGroup>
-        <PackageReference Include="EPPlus" Version="7.4.2" />
+        <PackageReference Include="EPPlus" Version="7.5.1" />
         <PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
     </ItemGroup>
     <ItemGroup>

+ 1 - 1
Masuit.Tools/Masuit.Tools.Net.csproj

@@ -35,7 +35,7 @@
 
     <ItemGroup>
         <PackageReference Include="Microsoft.AspNet.Mvc" Version="5.3.0" />
-        <PackageReference Include="StackExchange.Redis" Version="2.8.16" />
+        <PackageReference Include="StackExchange.Redis" Version="2.8.22" />
         <ProjectReference Include="..\Masuit.Tools.Abstractions\Masuit.Tools.Abstractions.csproj" />
         <Reference Include="System.Web" />
     </ItemGroup>

+ 1 - 1
NetCoreTest/NetCoreTest.csproj

@@ -6,7 +6,7 @@
     <ConcurrentGarbageCollection>false</ConcurrentGarbageCollection>
   </PropertyGroup>
   <ItemGroup>
-    <PackageReference Include="Swashbuckle.AspNetCore" Version="7.0.0" />
+    <PackageReference Include="Swashbuckle.AspNetCore" Version="7.1.0" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Controllers\" />

+ 1 - 1
Test/Masuit.Tools.Abstractions.Test/Masuit.Tools.Abstractions.Test.csproj

@@ -13,7 +13,7 @@
   </ItemGroup>
 
   <ItemGroup>
-    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
     <PackageReference Include="xunit" Version="2.9.2" />
     <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
       <PrivateAssets>all</PrivateAssets>

+ 1 - 1
Test/Masuit.Tools.AspNetCore.ResumeFileResults.WebTest/Masuit.Tools.AspNetCore.ResumeFileResults.WebTest.csproj

@@ -23,7 +23,7 @@
   </ItemGroup>
 
   <ItemGroup>
-    <PackageReference Include="Swashbuckle.AspNetCore" Version="7.0.0" />
+    <PackageReference Include="Swashbuckle.AspNetCore" Version="7.1.0" />
   </ItemGroup>
 
   <ItemGroup>

+ 1 - 1
Test/Masuit.Tools.Core.Test/Masuit.Tools.Core.Test.csproj

@@ -11,7 +11,7 @@
   <ItemGroup>
     <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="9.0.0" />
     <PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
-    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
     <PackageReference Include="xunit" Version="2.9.2" />
     <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
       <PrivateAssets>all</PrivateAssets>

+ 3 - 0
Test/Masuit.Tools.Test/Masuit.Tools.Test.csproj

@@ -96,6 +96,9 @@
     <PackageReference Include="SixLabors.ImageSharp">
       <Version>[2.1.9]</Version>
     </PackageReference>
+    <PackageReference Include="StackExchange.Redis">
+      <Version>2.8.22</Version>
+    </PackageReference>
     <PackageReference Include="System.Runtime.CompilerServices.Unsafe">
       <Version>6.1.0</Version>
     </PackageReference>