Dynamically Create WHERE Clause in Razor Page Linq

时间:2019-04-16 23:17:23

标签: linq razor-pages dynamic-linq

I see all sorts of examples that don't fit my scenario. I don't know how I got my LINQ to work to load up an Index View of all the rows. But now I want to filter the rows. I can hardcode a WHERE clause that will do this. However I need to dynamically build the WHERE clause at runtime, something I've done many times in other languages.

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();

Notice the [FieldName] in brackets. That is what I am trying to change dynamically. Depending on what is being sent in from the Select list. If selected, then I want the WHERE clause to bring back only rows where that column is not null:

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 not sure how to do this. My Linq query is a little different from all the examples in that the WHERE clause is at the end.

There are 4 CEU types. One can be picked from the pulldown. Then the code needs to use the column with that type to check if !=null.

1 个答案:

答案 0 :(得分:0)

Ok, I found that I was trying to add another ".Where" (which is possible) but not returning an iList which the data was sucked into. By adding ".ToList()" on the end of my dynamic I was able and will be able to add any dynamic ".Where" criteria I want.

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