none
How to Do, filtering operation manually on Complex DataTable RRS feed

  • Question

  • I have a DataTable in Home Controller as follows:

    public DataTable GetTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(Info));
        table.Columns.Add("Date", typeof(DateTime));
    
        table.Rows.Add(25, "Indocin", new Info("India"), DateTime.Now);
        table.Rows.Add(50, "Enebrel", new Info("UK"), DateTime.Now);
        table.Rows.Add(10, "Hydralazine", new Info("Bhutan"), DateTime.Now);
        table.Rows.Add(21, "Combivent", new Info("India"), DateTime.Now);
        table.Rows.Add(100, "Dilantin", new Info("GreenLand"), DateTime.Now);
        return table;
    }

    Info class as follows

    public class Info
    {
        public string Address { get; set; }
        public Info(string Add) {
            this.Address = Add;
        }
    }

    Now, i want to do the filtering operation based on Address Field, i.e Patient.Address

    Here, Patient is object of Info class

    I fetched the information regarding each columns as follows I have defined the functionalities in the method "DataTableExtensions.DataTableSerialize(data)"

    DataTableExtensions.DataTableSerialize(data)
    Count = 4
        [0]: {[Dosage, System.Nullable`1[System.Int32]]}
        [1]: {[Drug, System.String]}
        [2]: {[Patient, MvcApplication66.Controllers.HomeController+Info]}
        [3]: {[Date, System.Nullable`1[System.DateTime]]}

    But, i need to filter the DataTable based on Patient.Address

    I got the error in the following line.

    DataTableExtensions.DataTableSerialize(data)[ColumnName]

    An unhandled exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll

    Additional information: The given key was not present in the dictionary.

    Where i commit the mistake.

    Is it possible to do filtering operation in Complex Data Table

    Thursday, December 22, 2016 5:42 AM

All replies

  • Hi Kalai Selvi Rajendran,

    Based on your error, you used a Dictionary in your project but there is no operation for a Dictionary in your code? This is also another strange thing. What about removing all dictionary codes if you didn't do any operation with it. then, try to rebuild and run the project.

    DataTableExtensions.DataTableSerialize(data)[ColumnName]

    By the way, how do you define data?

    >>Now, i want to do the filtering operation based on Address Field, i.e Patient.Address

    According to your previous thread, I think there is no need to use DataTableExtensions.DataTableSerialize method. You could use the following code to query based on Address field.

      var ss = from myrow in table.AsEnumerable()
                         where myrow.Field<string>("Drug") == "Indocin"
                         select myrow;

    Here you can also refer to https://msdn.microsoft.com/en-us/library/bb386921%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 for more details.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Kristin Xie Thursday, December 22, 2016 9:38 AM
    Thursday, December 22, 2016 9:37 AM
  • Hi Kristin,

    As you said i have removed Dictionary related works. 

    My requirements to form the general predicate in order to fetch the data from DataTable.

    the code snippet which you shared, you done filtering for normal field "Drug". But, i want to filter the DataTable using Patient.Address.

    Is it possible to filters the DataTable like this.?

    Regards

    Kalai

    Thursday, December 22, 2016 9:42 AM
  • Hi Kalai,

    >>the code snippet which you shared, you done filtering for normal field "Drug". But, i want to filter the DataTable using Patient.Address.

    OK, actually, It is same.

    var ss = from myrow in table.AsEnumerable()
                         where myrow.Field<Info>("Patient").Address== "India"
                         select myrow;
     

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 23, 2016 6:29 AM