我正在将应用程序迁移到 .net 核心。但 SqlFunctions.StringConvert() 在 .netcore 中不起作用。
我正在使用以下代码:
using (var entity = new AppEntity())
{
var physianAggregationList = (from physianAggregaton in entity.tbl_Physician_Aggregation_Year
where (
(physianAggregaton.CMS_Submission_Year == cmsyear)
&& (physianAggregaton.Physician_NPI == npi || (string.IsNullOrEmpty(npi)))
&& physianAggregaton.Is_90Days == is_90Days && physianAggregaton.Exam_TIN == Tin
)
select new Tindata
{
Performance_Rate = entity.tbl_Lookup_Measure.Where(i => i.Measure_num.ToLower() == physianAggregaton.Measure_Num.ToLower()
&& i.CMSYear == physianAggregaton.CMS_Submission_Year)
.Select(x => x.Perf_Rate_Per_1000).FirstOrDefault() == boolValue
? (physianAggregaton.Performance_rate == null ? "NULL" : SqlFunctions.StringConvert(physianAggregaton.Performance_rate, 100, 2))
: (physianAggregaton.Performance_rate == null ? "NULL" : SqlFunctions.StringConvert(physianAggregaton.Performance_rate, 100, 2) + "%"),
Reporting_Rate = physianAggregaton.Reporting_Rate == null ? "NULL" : SqlFunctions.StringConvert(
//}).Distinct().OrderBy(x=>x.displayorder).ToList();
}).Distinct().OrderBy(x => x.LastName).ToList();
///logger.LogInfo("PhysianAggregation Count in getPhysianAggregationData():[" + physianAggregationList.Count()+"]");
}
最佳答案
SqlFunctions
是 EF6 特定的类,不能在 EF Core 中使用。 EF Core 自定义函数可用作 EF.Functions
的扩展方法.
不幸的是,目前 EF Core 不提供等效的 StringConvert
.但是通过使用 EF Core Database scalar function mapping 可以相对容易地添加它并映射到 STR
方法类似于EF6正在做的事情。
例如,添加以下类(带有必要的 using
s):
public static class SqlFunctions
{
public static string ToString(this decimal? value, int? length, int? decimalArg) => throw new NotSupportedException();
public static string ToString(this double? value, int? length, int? decimalArg) => throw new NotSupportedException();
public static ModelBuilder AddSqlFunctions(this ModelBuilder modelBuilder) => modelBuilder
.MapToSTR(() => ToString(default(decimal?), null, null))
.MapToSTR(() => ToString(default(double?), null, null));
static ModelBuilder MapToSTR(this ModelBuilder modelBuilder, Expression<Func<string>> method)
{
modelBuilder.HasDbFunction(method).HasTranslation(args =>
new SqlFunctionExpression(null, null, "STR", false, args, true, typeof(string), null));
return modelBuilder;
}
}
那么下面你的
OnModelCreating
里面覆盖:if (Database.IsSqlServer()) modelBuilder.AddSqlFunctions();
然后在查询中,替换
SqlFunctions.StringConvert(physianAggregaton.Performance_rate, 100, 2)
和
physianAggregaton.Performance_rate.ToString(100, 2)
关于c# - SqlFunctions.StringConvert() 不工作 ef 核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60301606/