dimanche 21 août 2022

how to write dynamic LINQ, using reflection to check property value and sum base on it

I have table in database and class for it

public class LocalDefaultIndicatorCostsDto
    {
        [Column("ReportingYear")]
        public int YearNo { get; set; }
        [Column("Quarter")]
        public int Quarter { get; set; }
        [Column("tur/exc")]
        public int TurOrExc { get; set; }
        [Column("VisitGoalID_New")]
        public double VisitGoalIDNew { get; set; }
        [Column("VisitGoalName_New")]
        public string VisitGoalNameNew { get; set; }
        [Column("ActivityStatusID_New")]
        public double ActivityStatusIDNew { get; set; }
        [Column("ActivityStatusName_New")]
        public string ActivityStatusNameNew { get; set; }
        [Column("RateID")]
        public int RateID { get; set; }
        [Column("RateName")]
        public string RateName { get; set; }
        [Column("GenderID")]
        public int GenderID { get; set; }
        [Column("GenderName")]
        public string GenderName { get; set; }
        [Column("Age_gr_id")]
        public double AgeGrID { get; set; }
        [Column("Age_gr")]
        public string AgeGr { get; set; }
        [Column("xarji_mln_lari")]
        public double? XarjiMlnLari { get; set; }
    }

and I want to sum property XarjiMlnLari, for year 2021 and ActivityStatusIDNew == 1. I have this LINQ, but it not worcking (error massage - could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.)

PropertyInfo propInfo = typeof(LocalDefaultIndicatorCostsDto).GetProperty("ActivityStatusIDNew");

var dicValue = await portalContext.LDIC
                                            .Where(x => x.YearNo == 2021 && 
                                            (double)propInfo.GetValue(x, null) == (double)1)
                                            .SumAsync(x => x.XarjiMlnLari);

thanks for advance





Aucun commentaire:

Enregistrer un commentaire