skip to Main Content

I want to apply multiple conditions in Datatable.Select query how can I do this.

Below is the code that I have:

dvOrg.Table.Select("customer_type_id='64501'", "sort_column");

Now I want to apply one more condition covered_patient_type='Inpatient'.
I tried this but it did not work.

dvOrg.Table.Select("customer_type_id='64501' and covered_patient_type='Inpatient'", "sort_column");

Please help.

Thanks in advance.

2

Answers


  1. The code for this is correct, which means the problem will be related to the specific data schema and the data types.

    This fiddle proves the concept, selecting by multiple columns is similar to SQL WHERE clause, using AND to append additional column criteria. In this OP is correct.

    https://dotnetfiddle.net/7cPr5H

    dvOrg.Table.Select("customer_type_id='64501' and covered_patient_type='Inpatient'", "id DESC");
    

    This works only if the covered_patient_type is implicitly castable to a string.

    For this data, we can see that customer_type_id is an int, but in the filter we can expect it to be a string and the filter condition still works:

    [{"id":1623,"customer_type_id":645,"covered_patient_type":"Outpatient","expectation":"filtered out"},
    {"id":1624,"customer_type_id":645,"covered_patient_type":"Inpatient","expectation":"filtered out"},
    {"id":1625,"customer_type_id":64501,"covered_patient_type":"Outpatient","expectation":"filtered out"},
    {"id":1626,"customer_type_id":64501,"covered_patient_type":"Inpatient","expectation":"selected"},
    {"id":1627,"customer_type_id":64501,"covered_patient_type":"Outpatient","expectation":"filtered out"},
    {"id":1628,"customer_type_id":64501,"covered_patient_type":"Inpatient","expectation":"selected"},
    {"id":1629,"customer_type_id":64501,"covered_patient_type":"Outpatient","expectation":"filtered out"}] 
    
    Filter By two columns: customer_type_id='64501' and covered_patient_type='Inpatient'
    ID:1628, expectation:selected, customer_type_id:64501, covered_patient_type:Inpatient
    ID:1626, expectation:selected, customer_type_id:64501, covered_patient_type:Inpatient
    

    The common scenario where this isn’t straight forward are when covered_patient_type is stored as an integer (or some other type where the value is a reference to another list, or enum and you are resolving the visible value in the view.

    In that case you need to filter on the underlying value, if "Inpatient" in the view is an enum, cast it to an int, or if "Inpatient" is a vlaue from a related table or otherwise unreleated list, resolve the pointer value that matches the value used int underlying DataTable dataset.

    All of this is easily resolved by including the raw DataTable in your original post. But I hope this thought process will help you explore these concepts on your own.

    Login or Signup to reply.
  2. If your code still doesn’t work, try building up the filter by adding conditions one by one.

    dvOrg.RowFilter = "customer_type_id='64501'";
    dvOrg.RowFilter += " and covered_patient_type='Inpatient'";
    dvOrg.Sort = "sort_column";
    

    And just double-check whether your data types are in the correct form.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search