where condition not a field .net

where condition not a field .net

montoyammontoyam Posts: 568Questions: 136Answers: 5
edited February 2020 in Free community support

I am filtering a second datatable based off a selected row of the first database. However, if there is nothing selected on the first datatable, I would like the second datatable to show all records. Is this possible? Currently, if unselected I am settting it to -1.

            ajax: {
                url: '/api/Contacts',
                type: 'post',
                data: function (d) {
                    var selected = applicationtable.row({ selected: true });
                    if (selected.any()) {
                        d['Contacts.ApplicationID'] = selected.data().ApplicationID;
                    } else {
                        d['Contacts.ApplicationID'] = -1;
                    }
                }
            },

it doesn't like the orWhere because it is expecting a fieldname

                                .Where(q =>
                                {
                                    q.Where(r =>
                                    {
                                        r.Where("Contacts.ApplicationID", request.Form["Contacts.ApplicationID"]);
                                        r.OrWhere(request.Form["Contacts.ApplicationID"],"-1");
                                    });
                                })

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,715Questions: 1Answers: 10,108 Site admin
    edited February 2020 Answer ✓

    I'd say only add the Where condition if the value of the application id is not -1, unless I've misunderstood?

    if(request.Form["Contacts.ApplicationID"] != "-1") {
      q.Where(...);
    }
    

    Allan

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    thank you so much. I should have thought of that!!

                                    .Where(q =>
                                    {
                                        if (request.Form["Contacts.ApplicationID"] != "-1")
                                        {
                                            q.Where("Contacts.ApplicationID", request.Form["Contacts.ApplicationID"]);
                                        }
                                    })
    
This discussion has been closed.