Filter Json from the bank using Lambda C#

Asked

Viewed 128 times

0

I’m starting development and I’m in need of some help. I got this Json I’m getting from the bank. But I need to filter the information by name for my return method. E.g. If the name = "Percent Processor Time", it should only return the array of values attached to it.

[
{
    "name": "Percent Processor Time",
    "jsonValues": [
        {
            "time": "2018-01-22T18:10:30Z",
            "instanceName": "RD00155D00C625",
            "value": 15.3125
        }
    ]
},
{
    "name": "Requests per Sec",
    "jsonValues": [
        {
            "time": "2018-01-22T18:10:30Z",
            "instanceName": "RD00155D00C625",
            "value": 8.5666666666666664
        }            
    ]
},
{
    "name": "Handles",
    "jsonValues": [
        {
            "time": "2018-01-22T18:11:00Z",
            "instanceName": "RD00155D00C625",
            "value": 2221.0
        }
    ]
},
{
    "name": "Threads",
    "jsonValues": [
        {
            "time": "2018-01-22T18:11:00Z",
            "instanceName": "RD00155D00C625",
            "value": 69.0
        }
    ]
},

My method is as follows.

public List<JsonValue> GetPerformance(DateTime StartDate, DateTime EndDate, string Name)
{
  var db = new Core.Database.EntitiesContext();
  var performance = new PerformanceInfo();
  performance.MarkTraceDates();
  List<JsonValue> retorno = null;//Criando lista para armazenar dados

  try
  {        
    var resultSearch = db.Performances.OrderByDescending(x => x.CreatedDate).First();//buscando no banco

    retorno = JsonConvert.DeserializeObject<List<JsonValue>>(resultSearch.Json);//deserializando Json na lista

  }
  catch (Exception e)
  {
    Console.Write(e.Message);
  }

  List<JsonValue> filtrado = new List<JsonValue>();
  filtrado = retorno.Select(x => new JsonValue
  {
    Name = x.Name,
    JsonValues = x.Properties.Data.Values.Select(y => new JsonValues { InstanceName = y.InstanceName, Time = y.Time, Value = y.Value }).ToList()
  }).ToList();

Someone can help me?

1 answer

0


You can change the section

 filtrado = retorno.Select(x => new JsonValue
  {
    Name = x.Name,
    JsonValues = x.Properties.Data.Values.Select(y => new JsonValues { InstanceName = y.InstanceName, Time = y.Time, Value = y.Value }).ToList()
  }).ToList();

For

 filtrado = retorno.Select(x => new JsonValue
      {
        Name = x.Name,
        JsonValues = x.Properties.Data.Values.Select(y => new JsonValues { InstanceName = y.InstanceName, Time = y.Time, Value = y.Value }).ToList()
      }).Where(a=> a.Name.Equals(Name)).ToList();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.