I'm getting a result from an API then deserialize it and I want to sort the JSON but it doesn't work and gives me an error: Cannot implicitly convert type 'System.Collections.Generic.List...
my API :
public async Task<Root_SDL> ValidateScenario(string SDL_text)
{
try
{
// Api Config
var ApiPath = "something here";
var SDL_version = "something here";
// var SDL_text = something here;
HttpClient httpClient_ = new HttpClient();
var Keys = new List<KeyValuePair<string, string>>();
Keys.Add(new KeyValuePair<string, string>("something here", SDL_version));
Keys.Add(new KeyValuePair<string, string>("something here", SDL_text));
var ApiRequest = new HttpRequestMessage(HttpMethod.Get, ApiPath) { Content = new FormUrlEncodedContent(Keys) };
HttpResponseMessage ApiResponse = await httpClient_.SendAsync(ApiRequest);
var responseString_ApiSDL = await ApiResponse.Content.ReadAsStringAsync();
// ParserIssuesToJS = responseString_ApiSDL;
var APIstatusCode = ApiResponse.StatusCode;
// API Deserialize
Root_SDL SDL = JsonConvert.DeserializeObject<Root_SDL>(responseString_ApiSDL);
var test = (from c in SDL.issues select c)
.Select(x => new
{
x.description,
x.columnStart,
x.columnEnd,
x.lineStart,
x.lineEnd
}
)
.OrderBy(x=>x.columnStart)
.ToList()
;
return test ; // here is the error (when I return SDL it works well but if I put 'test' that I miss a case or something)
}
catch (System.Exception)
{
throw;
}
}
below is my json. I need from it the 'passed' value as well as the the issues array
{
"Name": "something here",
"passed": false,
"issues": [
{
"description": "something here",
"lineStart": 13,
"lineEnd": 13,
"columnStart": 4,
"columnEnd": 10
},
{
"description": "something here",
"lineStart": 14,
"lineEnd": 14,
"columnStart": 30,
"columnEnd": 31
},
{
"description": "something here",
"lineStart": 15,
"lineEnd": 15,
"columnStart": 29,
"columnEnd": 30
},
{
"description": "mismatched input '.' expecting ':'",
"lineStart": 15,
"lineEnd": 15,
"columnStart": 36,
"columnEnd": 37
},
{
"description": "something here",
"lineStart": 14,
"lineEnd": 14,
"columnStart": 28,
"columnEnd": 30
},
{
"description": "something here",
"lineStart": 15,
"lineEnd": 15,
"columnStart": 27,
"columnEnd": 29
},
{
"description": "something here",
"lineStart": 15,
"lineEnd": 15,
"columnStart": 34,
"columnEnd": 36
}
]
}
I want to sort it by columnStart
below is my Root_SDL class
public class Root_SDL
{
public string? fileName { get; set; }
public bool passed { get; set; }
public string? parsingStartTime { get; set; }
public string? parsingEndTime { get; set; }
public List<Issue>? issues { get; set; }
public List<string>? debug { get; set; }
}
CodePudding user response:
This part of code which you return will not be a type of Root_SDL
, if you want to return that will not match with your return type from your method.
var test = (from c in SDL.issues select c)
.Select(x => new
{
x.description,
x.columnStart,
x.columnEnd,
x.lineStart,
x.lineEnd
}).OrderBy(x=>x.columnStart).ToList()
From your question, you can only order by
issues collection and directly, that might only sort issues
but reserve original property from SDL
Root_SDL SDL = JsonConvert.DeserializeObject<Root_SDL>(responseString_ApiSDL);
SDL.issues = SDL.issues.OrderBy(x=> x.columnStart).ToList();
return SDL;