我们发现只修补入站 Linq2Sql 代码是最简单的。我们还注意到 DbFunctions.AddMinutes() 只接受 Int32,而 DateTime.AddMinutes() 接受双精度。这可能会破坏预期的行为,因此我们也对其进行了修补。使用一些正则表达式和一些简单的字符串操作,得到了这个补丁代码。
这可能不适用于所有人,但如果您从 Linq2Sql 出发并且保存了遵循 Linq2Sql 的查询并且需要修补 DateTime 表达式...这将适用于 AddMintues、AddDays、AddHours、AddMilliseconds
private static string Linq2SqlConvertToEntityFramework(string originalQuery)
{
// Finds expressions with .Add____)
const string dateTimeAdditationPattern = @"(@?[a-z_A-Z]\w*(?:\.@?[a-z_A-Z]\w*)*).Add\s*.+?(?=\))\)";
// Finds all the matches
var matchces = Regex.Matches(originalQuery, dateTimeAdditationPattern);
// Enumerates all the matches, and creates a patch
foreach (Match m in matchces)
{
var inputValue = m.Value;
string valuePassed = inputValue.Between("(", ")").Trim();
string typeOfAddition = inputValue.Between(".Add", "(").Trim();
string dateTimeExpression = inputValue.Before(".Add").Trim();
// because DateTime.AddMinutes() (or any other AddXXX(Y) accepts a double, and
// DbFunctions only accepts an int,
// We must move this to milliseconds so we dont lose any expected behavior
// The input value could be an int or a input variable (such as a sub query)
var mutipler = 1;
switch (typeOfAddition)
{
case "Seconds":
mutipler = 1000;
break;
case "Minutes":
mutipler = 1000*60;
break;
case "Hours":
mutipler = 1000 * 60 * 60;
break;
case "Days":
mutipler = 1000 * 60 * 60 * 24;
break;
}
// new expression to work with Entity Framework
var replacementString = string.Format("DbFunctions.AddMilliseconds({0},(int)({1} * {2}))", dateTimeExpression, valuePassed, mutipler);
// Simple string replace for the input string
originalQuery = originalQuery.Replace(inputValue, replacementString);
}
return originalQuery;
}
/// <summary>
/// Get string value between [first] a and [last] b.
/// Credit Source: http://www.dotnetperls.com/between-before-after
/// </summary>
public static string Between(this string value, string a, string b)
{
int posA = value.IndexOf(a, StringComparison.InvariantCulture);
int posB = value.LastIndexOf(b, StringComparison.InvariantCulture);
if (posA == -1)
{
return "";
}
if (posB == -1)
{
return "";
}
int adjustedPosA = posA + a.Length;
if (adjustedPosA >= posB)
{
return "";
}
return value.Substring(adjustedPosA, posB - adjustedPosA);
}
/// <summary>
/// Get string value after [first] a.
/// Credit Source: http://www.dotnetperls.com/between-before-after
/// </summary>
public static string Before(this string value, string a)
{
int posA = value.IndexOf(a, StringComparison.InvariantCulture);
if (posA == -1)
{
return "";
}
return value.Substring(0, posA);
}
/// <summary>
/// Get string value after [last] a.
/// Credit Source: http://www.dotnetperls.com/between-before-after
/// </summary>
public static string After(this string value, string a)
{
int posA = value.LastIndexOf(a, StringComparison.InvariantCulture);
if (posA == -1)
{
return "";
}
int adjustedPosA = posA + a.Length;
if (adjustedPosA >= value.Length)
{
return "";
}
return value.Substring(adjustedPosA);
}