## v0.12.11 调整 ContainsMany 方法名为 Contains

This commit is contained in:
28810
2019-12-08 13:04:29 +08:00
parent d186affe73
commit dee1d9af8b
18 changed files with 222 additions and 198 deletions

View File

@ -0,0 +1,130 @@
using FreeSql;
using FreeSql.DataAnnotations;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading;
[ExpressionCall]
public static class FreeSqlGlobalExpressionCall
{
public static ThreadLocal<ExpressionCallContext> expContext = new ThreadLocal<ExpressionCallContext>();
/// <summary>
/// C# that >= between &amp;&amp; that &lt;= and<para></para>
/// SQL that BETWEEN between AND and
/// </summary>
/// <param name="that"></param>
/// <param name="between"></param>
/// <param name="and"></param>
/// <returns></returns>
public static bool Between(this DateTime that, DateTime between, DateTime and)
{
if (expContext.IsValueCreated == false || expContext.Value == null || expContext.Value.ParsedContent == null)
return that >= between && that <= and;
expContext.Value.Result = $"{expContext.Value.ParsedContent["that"]} between {expContext.Value.ParsedContent["between"]} and {expContext.Value.ParsedContent["and"]}";
return false;
}
/// <summary>
/// 注意:这个方法和 Between 有细微区别<para></para>
/// C# that >= start &amp;&amp; that &lt; end<para></para>
/// SQL that >= start and that &lt; end
/// </summary>
/// <param name="that"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
public static bool BetweenEnd(this DateTime that, DateTime start, DateTime end)
{
if (expContext.IsValueCreated == false || expContext.Value == null || expContext.Value.ParsedContent == null)
return that >= start && that < end;
expContext.Value.Result = $"{expContext.Value.ParsedContent["that"]} >= {expContext.Value.ParsedContent["start"]} and {expContext.Value.ParsedContent["that"]} < {expContext.Value.ParsedContent["end"]}";
return false;
}
/// <summary>
/// C#:从元组集合中查找 exp1, exp2 是否存在<para></para>
/// SQL <para></para>
/// exp1 = that[0].Item1 and exp2 = that[0].Item2 OR <para></para>
/// exp1 = that[1].Item1 and exp2 = that[1].Item2 OR <para></para>
/// ... <para></para>
/// 注意:当 that 为 null 或 empty 时,返回 1=0
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="that"></param>
/// <param name="exp1"></param>
/// <param name="exp2"></param>
/// <returns></returns>
public static bool Contains<T1, T2>([RawValue] this IEnumerable<(T1, T2)> that, T1 exp1, T2 exp2)
{
if (expContext.IsValueCreated == false || expContext.Value == null || expContext.Value.ParsedContent == null)
return that?.Any(a => a.Item1.Equals(exp1) && a.Item2.Equals(exp2)) == true;
if (that?.Any() != true)
{
expContext.Value.Result = "1=0";
return false;
}
var sb = new StringBuilder();
var idx = 0;
foreach (var item in that)
{
if (idx++ > 0) sb.Append(" OR \r\n");
sb
.Append(expContext.Value.ParsedContent["exp1"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T1), item.Item1)))
.Append(" AND ")
.Append(expContext.Value.ParsedContent["exp2"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T2), item.Item2)));
}
expContext.Value.Result = sb.ToString();
return true;
}
/// <summary>
/// C#:从元组集合中查找 exp1, exp2, exp2 是否存在<para></para>
/// SQL <para></para>
/// exp1 = that[0].Item1 and exp2 = that[0].Item2 and exp3 = that[0].Item3 OR <para></para>
/// exp1 = that[1].Item1 and exp2 = that[1].Item2 and exp3 = that[1].Item3 OR <para></para>
/// ... <para></para>
/// 注意:当 that 为 null 或 empty 时,返回 1=0
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <typeparam name="T3"></typeparam>
/// <param name="that"></param>
/// <param name="exp1"></param>
/// <param name="exp2"></param>
/// <param name="exp3"></param>
/// <returns></returns>
public static bool Contains<T1, T2, T3>([RawValue] this IEnumerable<(T1, T2, T3)> that, T1 exp1, T2 exp2, T3 exp3)
{
if (expContext.IsValueCreated == false || expContext.Value == null || expContext.Value.ParsedContent == null)
return that.Any(a => a.Item1.Equals(exp1) && a.Item2.Equals(exp2) && a.Item3.Equals(exp3));
if (that.Any() == false)
{
expContext.Value.Result = "1=0";
return false;
}
var sb = new StringBuilder();
var idx = 0;
foreach (var item in that)
{
if (idx++ > 0) sb.Append(" OR \r\n");
sb
.Append(expContext.Value.ParsedContent["exp1"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T1), item.Item1)))
.Append(" AND ")
.Append(expContext.Value.ParsedContent["exp2"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T2), item.Item2)))
.Append(" AND ")
.Append(expContext.Value.ParsedContent["exp3"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T3), item.Item3)));
}
expContext.Value.Result = sb.ToString();
return true;
}
}

View File

@ -222,124 +222,4 @@ public static partial class FreeSqlGlobalExtensions
}
#endif
#endregion
#region LambdaExpression
public static ThreadLocal<ExpressionCallContext> expContext = new ThreadLocal<ExpressionCallContext>();
/// <summary>
/// C# that >= between &amp;&amp; that &lt;= and<para></para>
/// SQL that BETWEEN between AND and
/// </summary>
/// <param name="that"></param>
/// <param name="between"></param>
/// <param name="and"></param>
/// <returns></returns>
[ExpressionCall]
public static bool Between(this DateTime that, DateTime between, DateTime and)
{
if (expContext.IsValueCreated == false || expContext.Value == null || expContext.Value.ParsedContent == null)
return that >= between && that <= and;
expContext.Value.Result = $"{expContext.Value.ParsedContent["that"]} between {expContext.Value.ParsedContent["between"]} and {expContext.Value.ParsedContent["and"]}";
return false;
}
/// <summary>
/// 注意:这个方法和 Between 有细微区别<para></para>
/// C# that >= start &amp;&amp; that &lt; end<para></para>
/// SQL that >= start and that &lt; end
/// </summary>
/// <param name="that"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
[ExpressionCall]
public static bool BetweenEnd(this DateTime that, DateTime start, DateTime end)
{
if (expContext.IsValueCreated == false || expContext.Value == null || expContext.Value.ParsedContent == null)
return that >= start && that < end;
expContext.Value.Result = $"{expContext.Value.ParsedContent["that"]} >= {expContext.Value.ParsedContent["start"]} and {expContext.Value.ParsedContent["that"]} < {expContext.Value.ParsedContent["end"]}";
return false;
}
/// <summary>
/// C#:从元组集合中查找 exp1, exp2 是否存在<para></para>
/// SQL <para></para>
/// exp1 = that[0].Item1 and exp2 = that[0].Item2 OR <para></para>
/// exp1 = that[1].Item1 and exp2 = that[1].Item2 OR <para></para>
/// ... <para></para>
/// 注意:当 that 为 null 或 empty 时,返回 1=0
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="that"></param>
/// <param name="exp1"></param>
/// <param name="exp2"></param>
/// <returns></returns>
[ExpressionCall]
public static bool ContainsMany<T1, T2>([RawValue] this IEnumerable<(T1, T2)> that, T1 exp1, T2 exp2)
{
if (expContext.IsValueCreated == false || expContext.Value == null || expContext.Value.ParsedContent == null)
return that?.Any(a => a.Item1.Equals(exp1) && a.Item2.Equals(exp2)) == true;
if (that?.Any() != true)
{
expContext.Value.Result = "1=0";
return false;
}
var sb = new StringBuilder();
var idx = 0;
foreach (var item in that)
{
if (idx++ > 0) sb.Append(" OR \r\n");
sb
.Append(expContext.Value.ParsedContent["exp1"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T1), item.Item1)))
.Append(" AND ")
.Append(expContext.Value.ParsedContent["exp2"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T2), item.Item2)));
}
expContext.Value.Result = sb.ToString();
return true;
}
/// <summary>
/// C#:从元组集合中查找 exp1, exp2 是否存在<para></para>
/// SQL <para></para>
/// exp1 = that[0].Item1 and exp2 = that[0].Item2 and exp3 = that[0].Item3 OR <para></para>
/// exp1 = that[1].Item1 and exp2 = that[1].Item2 and exp3 = that[1].Item3 OR <para></para>
/// ... <para></para>
/// 注意:当 that 为 null 或 empty 时,返回 1=0
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <typeparam name="T3"></typeparam>
/// <param name="that"></param>
/// <param name="exp1"></param>
/// <param name="exp2"></param>
/// <param name="exp3"></param>
/// <returns></returns>
[ExpressionCall]
public static bool ContainsMany<T1, T2, T3>([RawValue] this IEnumerable<(T1, T2, T3)> that, T1 exp1, T2 exp2, T3 exp3)
{
if (expContext.IsValueCreated == false || expContext.Value == null || expContext.Value.ParsedContent == null)
return that.Any(a => a.Item1.Equals(exp1) && a.Item2.Equals(exp2) && a.Item3.Equals(exp3));
if (that.Any() == false)
{
expContext.Value.Result = "1=0";
return false;
}
var sb = new StringBuilder();
var idx = 0;
foreach (var item in that)
{
if (idx++ > 0) sb.Append(" OR \r\n");
sb
.Append(expContext.Value.ParsedContent["exp1"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T1), item.Item1)))
.Append(" AND ")
.Append(expContext.Value.ParsedContent["exp2"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T2), item.Item2)))
.Append(" AND ")
.Append(expContext.Value.ParsedContent["exp3"]).Append(" = ").Append(expContext.Value.FormatSql(FreeSql.Internal.Utils.GetDataReaderValue(typeof(T3), item.Item3)));
}
expContext.Value.Result = sb.ToString();
return true;
}
#endregion
}

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
<Version>0.12.10</Version>
<Version>0.12.11</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>FreeSql is the most convenient ORM in .NetCore, .NetFramework, And Xamarin. It supports Mysql, Postgresql, SqlServer, Oracle, Sqlite, And Odbc.</Description>

View File

@ -2688,6 +2688,61 @@
<param name="str"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExpressionCall.Between(System.DateTime,System.DateTime,System.DateTime)">
<summary>
C# that >= between &amp;&amp; that &lt;= and<para></para>
SQL that BETWEEN between AND and
</summary>
<param name="that"></param>
<param name="between"></param>
<param name="and"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExpressionCall.BetweenEnd(System.DateTime,System.DateTime,System.DateTime)">
<summary>
注意:这个方法和 Between 有细微区别<para></para>
C# that >= start &amp;&amp; that &lt; end<para></para>
SQL that >= start and that &lt; end
</summary>
<param name="that"></param>
<param name="start"></param>
<param name="end"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExpressionCall.Contains``2(System.Collections.Generic.IEnumerable{System.ValueTuple{``0,``1}},``0,``1)">
<summary>
C#:从元组集合中查找 exp1, exp2 是否存在<para></para>
SQL <para></para>
exp1 = that[0].Item1 and exp2 = that[0].Item2 OR <para></para>
exp1 = that[1].Item1 and exp2 = that[1].Item2 OR <para></para>
... <para></para>
注意:当 that 为 null 或 empty 时,返回 1=0
</summary>
<typeparam name="T1"></typeparam>
<typeparam name="T2"></typeparam>
<param name="that"></param>
<param name="exp1"></param>
<param name="exp2"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExpressionCall.Contains``3(System.Collections.Generic.IEnumerable{System.ValueTuple{``0,``1,``2}},``0,``1,``2)">
<summary>
C#:从元组集合中查找 exp1, exp2, exp2 是否存在<para></para>
SQL <para></para>
exp1 = that[0].Item1 and exp2 = that[0].Item2 and exp3 = that[0].Item3 OR <para></para>
exp1 = that[1].Item1 and exp2 = that[1].Item2 and exp3 = that[1].Item3 OR <para></para>
... <para></para>
注意:当 that 为 null 或 empty 时,返回 1=0
</summary>
<typeparam name="T1"></typeparam>
<typeparam name="T2"></typeparam>
<typeparam name="T3"></typeparam>
<param name="that"></param>
<param name="exp1"></param>
<param name="exp2"></param>
<param name="exp3"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExtensions.Distance(System.Drawing.Point,System.Drawing.Point)">
<summary>
测量两个经纬度的距离,返回单位:米
@ -2773,61 +2828,6 @@
<param name="then">即能 ThenInclude还可以二次过滤这个 EFCore 做不到?)</param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExtensions.Between(System.DateTime,System.DateTime,System.DateTime)">
<summary>
C# that >= between &amp;&amp; that &lt;= and<para></para>
SQL that BETWEEN between AND and
</summary>
<param name="that"></param>
<param name="between"></param>
<param name="and"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExtensions.BetweenEnd(System.DateTime,System.DateTime,System.DateTime)">
<summary>
注意:这个方法和 Between 有细微区别<para></para>
C# that >= start &amp;&amp; that &lt; end<para></para>
SQL that >= start and that &lt; end
</summary>
<param name="that"></param>
<param name="start"></param>
<param name="end"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExtensions.ContainsMany``2(System.Collections.Generic.IEnumerable{System.ValueTuple{``0,``1}},``0,``1)">
<summary>
C#:从元组集合中查找 exp1, exp2 是否存在<para></para>
SQL <para></para>
exp1 = that[0].Item1 and exp2 = that[0].Item2 OR <para></para>
exp1 = that[1].Item1 and exp2 = that[1].Item2 OR <para></para>
... <para></para>
注意:当 that 为 null 或 empty 时,返回 1=0
</summary>
<typeparam name="T1"></typeparam>
<typeparam name="T2"></typeparam>
<param name="that"></param>
<param name="exp1"></param>
<param name="exp2"></param>
<returns></returns>
</member>
<member name="M:FreeSqlGlobalExtensions.ContainsMany``3(System.Collections.Generic.IEnumerable{System.ValueTuple{``0,``1,``2}},``0,``1,``2)">
<summary>
C#:从元组集合中查找 exp1, exp2 是否存在<para></para>
SQL <para></para>
exp1 = that[0].Item1 and exp2 = that[0].Item2 and exp3 = that[0].Item3 OR <para></para>
exp1 = that[1].Item1 and exp2 = that[1].Item2 and exp3 = that[1].Item3 OR <para></para>
... <para></para>
注意:当 that 为 null 或 empty 时,返回 1=0
</summary>
<typeparam name="T1"></typeparam>
<typeparam name="T2"></typeparam>
<typeparam name="T3"></typeparam>
<param name="that"></param>
<param name="exp1"></param>
<param name="exp2"></param>
<param name="exp3"></param>
<returns></returns>
</member>
<member name="M:System.Linq.Expressions.LambadaExpressionExtensions.And``1(System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}},System.Linq.Expressions.Expression{System.Func{``0,System.Boolean}})">
<summary>
使用 and 拼接两个 lambda 表达式