我正在将应用程序迁移到.net核心。但是SqlFunctions.StringConvert()不能在in.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()+"]");
}
发布于 2020-02-19 23:49:13
SqlFunctions
是特定于EF6的类,不能在EF核心中使用。EF核心自定义函数可用作EF.Functions
的扩展方法。
不幸的是,目前EF Core没有提供与StringConvert
相同的功能。但是通过使用EF核心Database scalar function mapping和映射到STR
方法可以相对容易地添加它,就像EF6所做的那样。
例如,添加以下类(带有必要的using
):
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)
https://stackoverflow.com/questions/60301606
复制相似问题