How to filter an array of objects using User defined function?

0 Answers 199 Views
.NET Core DataSource JSON Filtering General Discussions
Miftaul
Top achievements
Rank 1
Miftaul asked on 12 May 2022, 01:23 PM

Say I have a list of object and I want a field to show one specific value from those objects.


public class Data
{
  public List<Obj> Objects { get; set;}
}

public class Obj
{
  public string Name { get; set;}
  public int Id { get; set;}
}

 

Data reportData = [

     {Name: abc, Id: 4 },

     {Name: def, Id: 5 }

]

I want to show the Name (in a textbox) where Id = 5. How do I do that?

 

I tried doing it with a user function where I pass the array but it throws an error when I try to cast object back to Obj class that (webservice.jsonObject can't be cast to Obj class). If I can't cast the object to Obj class, how am I supposed to filter it?

Neli
Telerik team
commented on 17 May 2022, 11:04 AM

Hi Miftaul,

If you have assigned the datasource to the report, then you can add a List and set its Binding property as follows:

Property path: DataSource

Expression: =Fields.Objects.

Then, add textboxes for =Fields.Name and =Fields.Id. Finally, set the Filters property of the list.

Expression: =FIelds.Id

Operator: =

Value: =5

Miftaul
Top achievements
Rank 1
commented on 17 May 2022, 11:11 AM | edited

@Neli

Did something similar with table. But it's bothersome to do this for a simple filter. My datasourse has nested objects that I need to filter.

I wish I could pass the objects to a user function for filtering. I already have the classes but telerik sends these objects as JsonObject which I can't convert back into its proper class.

Neli
Telerik team
commented on 20 May 2022, 08:26 AM

Hi Miftaul,

Can you please send us a sample runnable project that reproduces the problem, so I can inspect it locally? Note that it is not necessary to include real data. Also, you can provide a screenshot of the desired outcome.

No answers yet. Maybe you can help?

Tags
.NET Core DataSource JSON Filtering General Discussions
Asked by
Miftaul
Top achievements
Rank 1
Share this question
or