Raju Gaddam
Raju Gaddam

Reputation: 78

SqlFunctions.StringConvert() is not working ef core

I am migrating the application to the .net core. but SqlFunctions.StringConvert() is not working in.netcore. I am using the following code:

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()+"]");
            } 

Upvotes: 5

Views: 3350

Answers (2)

dima-hx
dima-hx

Reputation: 3304

You can do it like this:

_dbContext.Some
    .Where(x => x.Id.ToString().Contains("123"));

Upvotes: 0

Ivan Stoev
Ivan Stoev

Reputation: 205589

SqlFunctions is EF6 specific class and cannot be used in EF Core. EF Core custom functions are available as extension methods of EF.Functions.

Unfortunately currently EF Core does not provide equivalent of StringConvert. But it can be added relatively easy by using the EF Core Database scalar function mapping and mapping to STR method similar to what EF6 was doing.

For instance, add the following class (with the necessary usings):

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;
    }
}

then the following inside your OnModelCreating override:

if (Database.IsSqlServer()) modelBuilder.AddSqlFunctions();

then inside the query, replace

SqlFunctions.StringConvert(physianAggregaton.Performance_rate, 100, 2)

with

physianAggregaton.Performance_rate.ToString(100, 2)

Upvotes: 6

Related Questions