This is a migrated thread and some comments may be shown as answers.

ToDataSourceResult with Dictionary

5 Answers 771 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeffrey
Top achievements
Rank 1
Jeffrey asked on 17 Aug 2016, 04:39 PM

For our application, we were using Kendo Grid with a class named Properties that stored column information:

public class Properties
{
   
public string Id { get; set; }
public object PROPERTY_0 { get; set; }
public object PROPERTY_1 { get; set; }
...
public object PROPERTY_43 { get; set; }

In order to sort and filter rows, we used the ToDataSourceResult extension. However, this system limited us to a hardcoded maximum number of columns. We have refactored the code to use a List<Dictionary<string, dynamic>> instead, which allows us to store results in key value pairs in a list thus allowing an arbitrary maximum number of columns.

Unfortunately, I have not been able to find any documentation on whether ToDataSourceResult is able to parse data in this format, or any format aside from the previous one. While it is possible to recreate the logic for filtering, grouping, and sorting, it would take a good deal of time to do so. Is there any way to use ToDataSourceResult instead? We are storing values in a dictionary with PROPERTY_x as a string key and a dynamic value.

5 Answers, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 19 Aug 2016, 11:45 AM
Hello,

I would suggest you to see the threads below for more information on that matter:

https://doylestowncoder.com/2014/04/14/kendoui-understanding-todatasourceresult/
blog.falafel.com/server-paging-sorting-filtering-kendo-datasourcerequest/

Regards,
Maria Ilieva
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Jeffrey
Top achievements
Rank 1
answered on 19 Aug 2016, 04:19 PM

Hi, thanks for your response. I've already reviewed multiple Google results including both of these, and none of them go into actual explanation of the syntax needed to pass additional options to the ToDataSourceResult extension.

What is happening is that grid passes a DataSourceRequest, and the FilterDescriptor contains a Member of the column name (in our system, it would be PROPERTY_2 or something similar). ToDataSourceResult seems to require a class with defined properties in order to execute its sorting logic. However, we have since converted from a class to a list of dictionaries, with each dictionary entry corresponding to a column and each item in the list corresponding to a row. ToDataSourceResult cannot seem to use the Member to filter the dictionary keys. There has been no documentation of this use case, or any examples of something similar.

Additionally, while working on a custom solution, another issue was discovered. If we pass multiple filters in a CompositeFilterDescriptor, the MemberType is null. This only occurs with more than one filter, and is causing incorrect casting of the filter value. Is there any way to fetch the MemberType from the request object with more than one filter present?

0
Maria Ilieva
Telerik team
answered on 23 Aug 2016, 02:29 PM
Hello,

Note that the ToDataSourceResult require a class with defined fields in order to execute sorting and filtering functionality. The MVC framework is working with models and the required functionality can not be achieved for  a list of dictionaries.
Possible appraoch is to map the dictionary collection with Select before the ToDataSourceResult extension is applied, which is almost the same as creating a Model:
public ActionResult EditingInline_Read([DataSourceRequest] DataSourceRequest request)
{
    List<Dictionary<string, dynamic>> test = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, dynamic>>();
    IEnumerable<ProductViewModel> products = productService.Read();
    foreach (var item in products)
    {
        Dictionary<string, dynamic> dict = new Dictionary<string, dynamic>();
        dict.Add("ProductID", item.ProductID);
        dict.Add("ProductName", item.ProductName);
        dict.Add("UnitPrice", item.UnitPrice);
        test.Add(dict);
    }
    var t = test.Select(item=> new ProductViewModel(){
        ProductName = item["ProductName"],
        ProductID = item["ProductID"],
        UnitPrice = item["UnitPrice"],
    }).ToDataSourceResult(request);
    return Json(t);           
}

I hope this helps.

Regards,
Maria Ilieva
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Mark
Top achievements
Rank 1
answered on 15 Aug 2019, 05:56 PM
Maria, what if I need to perform some operations on the data before I send the ActionResult back to the client?  I would like to use the DataSourceRequest with its filter to filter out the data in the controller, then I need to use that data to perform some other functions before I send it back to the client.  Having difficulties figuring out how to extract the data into an Ilist or IEnumerable.  Thanks.
0
Boyan Dimitrov
Telerik team
answered on 19 Aug 2019, 02:37 PM
Hello,

Actually calling the ToDataSourceResult(request) method will return an IQueryable that is not the real data loaded in the memory. In order to apply the expressions from the request object and load the data from the database the ToList() method should be called. Once you call the ToList() method the data will be loaded in the memory and it is available for you to apply any additional options to the data before it is returned to the client. 

.ToDataSourceResult(request).ToList();


Hope that this helps. 

Regards,
Boyan Dimitrov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Jeffrey
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Jeffrey
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Boyan Dimitrov
Telerik team
Share this question
or