How to filter objects using JsonServiceClient

//DTO public class SampleDto : IReturn<SampleDto> { public int Id { get; set; } public string Description { get; set; } } public class ListSampleDto : IReturn<List<SampleDto>> { } //Service public class SampleService : Service { public object Get(ListSampleDto request) { List<SampleDto> res = new List<SampleDto>(); res.Add(new SampleDto() { Id = 1, Description = "first" }); res.Add(new SampleDto() { Id = 2, Description = "second" }); res.Add(new SampleDto() { Id = 3, Description = "third" }); return res; } } //Client string ListeningOn = ServiceStack.Configuration.ConfigUtils.GetAppSetting("ListeningOn"); JsonServiceClient jsc = new JsonServiceClient(ListeningOn); // How to tell the service only to deliver the objects where Description inludes the letter "i" List<SampleDto> ks = jsc.Get(new ListSampleDto()); 

I do not know how to send filter criteria (for example, to get only objects in which the description includes the letter "i") from JsonServiceClient to the service.

+4
source share
1 answer

In this situation, you usually extend your Dto input (in this case ListSampleDto ) with properties that you evaluate on the server side to provide the correct answer:

 // Request Dto: public class ListSampleDto { public string Filter { get; set; } } ... // Service implementation: public object Get(ListSampleDto request) { List<SampleDto> res = new List<SampleDto>(); res.Add(new SampleDto() { Id = 1, Description = "first" }); res.Add(new SampleDto() { Id = 2, Description = "second" }); res.Add(new SampleDto() { Id = 3, Description = "third" }); if (!string.IsNullOrEmpty(request.Filter)) { res = res.Where(r => r.Description.StartsWith(request.Filter)).ToList() } return res; } ... // Client call: List<SampleDto> ks = jsc.Get(new ListSampleDto { Filter = "i" }); 
+2
source

All Articles