Dynamically Create WHERE Clause in Razor Page Linq

dynamic-linq linq razor-pages

Question

I come across a lot of instances that don't apply to my situation. I'm not sure how I managed to get my LINQ to load up an Index View of every record. The rows need to be filtered, however. This may be accomplished by hardcoding a WHERE clause. However, as I've done several times in other languages, I must dynamically construct the WHERE clause at runtime.

Courses = await _context.Courses
    .Select(p => new CoursesVM
    {
        OESACID = p.OESACID,
        CourseTitle = p.CourseTitle,
        Instructor = p.Instructor,
        Locations = p.Locations,
        Dates = p.Dates,
        CEUDWP = p.CEUDWP,
        CEUDEQ = p.CEUDEQ,
        CEUonsiteInstall = p.CEUonsiteInstall,
        CEUonsiteOandM = p.CEUonsiteOandM,
        MaxCEU = p.MaxCEU,
        SponsorID = p.SponsorID,
        MrMs = p.MrMs,
        CurrentContactName = p.CurrentContactName,
        ContactBizName = p.ContactBizName,
        ContactAddress = p.ContactAddress,
        ContactCity = p.ContactCity,
        ContactState = p.ContactState,
        ContactZip = p.ContactZip,
        CurrentContactPhone = p.CurrentContactPhone,
        CurrentContactFax = p.CurrentContactFax,
        CurrentContactEmail = p.CurrentContactEmail,
        DateRec = p.DateRec,
        FeeRec = p.FeeRec,
        CheckNumber = p.CheckNumber,
        PrelimAprvDate = p.PrelimAprvDate,
        MailedReceipt = p.MailedReceipt,
        FinalAprvDate = p.FinalAprvDate,
        MailedFinal = p.MailedFinal,
        HomeStudy = p.HomeStudy,
        Recurring = p.Recurring,
        PutOnCommitteeList = p.PutOnCommitteeList,
        FinalLetterSent = p.FinalLetterSent,
        URL = p.URL,
        //ThreeYearLetterSent = p.ThreeYearLetterSent,
        TakeOffWeb = p.TakeOffWeb,
        Inactive = p.Inactive
    }).Where(p => [FieldName]!= null && p.FinalAprvDate >= threeYearsAgo).OrderByDescending(p => p.OESACID).ToListAsync();

The [FieldName] is enclosed in brackets. I'm attempting a dynamic modification in it. Depending on the selections that are delivered from the Select list. If chosen, I want the WHERE clause to only return rows with that column's value present:

public async Task OnGetAsync(String CEUType)
enter code here
   switch(CEUType)
        {
            case "DWP":
    what goes here???
                break;
            case "DEQ":
    what goes here???
                break;

            case "OnSite":
    what goes here???
                break;
            case "HomeStudy":
    what goes here???
                break;
        }

I'm unsure on how to proceed. The WHERE clause in my Linq query is at the end, which is a bit different from all the samples.

There are 4 different CEU categories. You may choose one from the pulldown menu. The code must then utilize the column for that type to determine whether!=null.

1
0
4/16/2019 11:17:22 PM

Popular Answer

Okay, I realized that I was attempting to add another ".Where" when it was really feasible to do so without delivering an iList, which is where the data got pulled in. I was able to add whatever dynamic ".Where" criterion I wanted by putting ".ToList()" to the end of my dynamic.

at the top:

   Courses = await _context.Courses
                .Select(p => new CoursesVM
                {
                    OESACID = p.OESACID,
                    CourseTitle = p.CourseTitle,

at the bottom:

                }).Where(p =>  p.FinalAprvDate >= threeYearsAgo).OrderByDescending(p => p.OESACID).ToListAsync();



            switch (CEUType)
            {
                case "DWP":
                    Courses = Courses.Where(p => p.CEUDWP != null).ToList();
                    break;
                case "DEQ":

                    break;

                case "OnSite":

                    break;
                case "HomeStudy":

                    break;
            }
0
4/17/2019 1:32:49 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow