Home > Blockchain >  How to convert a linq query to ToList() to use extended properties
How to convert a linq query to ToList() to use extended properties

Time:02-24

 private IQueryable<SubjectDataViewModel> GetData()
    {
        var subject = this.DbContext.Subjects.AsQueryable();  //sql view 

        

        var data =
            from subject in subjects
            let treatment = this.DbContext.TreatmentFactor.FirstOrDefault(x => x.TreatmentFactorId == subject.ActiveTreatmentFactor)
            select new SubjectDataViewModel
                       {
                           Name = subject.Name,
                           Gender = subject.Gender,
                           Version = subject.Version.IsEnumToValueDisplay()   // extented method which displays enum DisplayName property into string 
                           
                       };
        

        return data.AsQueryable();
    }
    

This is the View Model i am populating into as per query above .

     public class SubjectDataViewModel
{
     public string Name {get;set;}
     public string Gender {get;set;}
     public string Version {get;set;}   // string type return
}

This is Subject Sql View

     public class Subjects
{
     public string Name {get;set;}
     public string Gender {get;set;}
     public Version Version {get;set;}   // enum return type 
}

Enum that is used for Version . Need to Used the ToEnumDiplayValue() extended method in the linq query to pull the Diplay Name i.e Original instead of Database value which is 0 and 1 which will show first or second.

  public enum Version
{
    [Display(Name = "Original")]
    first= 0,

    [Display(Name = "Not Original")]
    second = 1,
}

So I want to get the DisplayName for the Version which can be done by using our own extended method that using reflection to get the Display name from the enum . But i cannot use it in the in memoryobject . It has to be converted to .ToList() for. That is what i want. I hope i am clear. Thank you for your time . As of now my code will throw ERROR message since i cannot use extended property in Linq.

CodePudding user response:

Try

private IQueryable<SubjectDataViewModel> GetData()
    {
        var subject = this.DbContext.Subjects.AsQueryable();  //sql view 

        

        var data =
            from subject in subjects
            let treatment = this.DbContext.TreatmentFactor.FirstOrDefault(x => x.TreatmentFactorId == subject.ActiveTreatmentFactor)
            select new SubjectDataViewModel
                       {
                           Name = subject.Name,
                           Gender = subject.Gender,
                           VersionEnum = subject.Version // Version Enum
                           
                       };
        

        return data.AsQueryable();
    }

Use below code to convert GetData() into the list afterwards use IsEnumToValueDisplay() method.

var list = GetData().ToList();
foreach (var item in list)
{
 item.Version = item.VersionEnum.IsEnumToValueDisplay(); // Get value from Version Enum
}

Change Model SubjectDataViewModel

public class SubjectDataViewModel
{
     public string Name {get;set;}
     public string Gender {get;set;}
     public Version VersionEnum {get;set;}  
     public string Version {get;set;} 
}
  • Related