Home > Net >  C# LINQ to select element from string array
C# LINQ to select element from string array

Time:11-24

I have a string:

private const string Codes = "2,10";

public void method()
{
    var displayCodes = Codes.Split(',');
    DemoResponse response = webService.GetResponse();  //getting response from API

    if(response.Errors.Any(x => displayCodes.Contains(x.StackTrace))
    {
         int myCode = int.Parse(response.Errors.Select(x => x.StackTrace).FirstOrDefault());
    }
}

This is the "DemoResponse" model:

public class DemoResponse
{
     public bool Validate { get; set; }
     public IEnumerable<ErrorResponse> Errors { get; set; }
}

public class ErrorResponse
{
     public string Message { get; set; }
     public string StackTrace { get; set; }
}

The DemoResponse "response" from the API returns result as: (for example)

 { 
    "Validate" : false,
    "Errors" : 
             {
                "Message" : "test1",
                "StackTrace" : "2"
             }
  }
  { 
     "Validate" : false,
     "Errors" : 
             {
                "Message" : "test1",
                "StackTrace" : "2"
             }
  }
  { 
     "Validate" : false,
     "Errors" : 
             {
                "Message" : "test1",
                "StackTrace" : "95"
             }
  }

The DemoResponse "response" will always contain only one of the code from const string "Codes", i.e either 2 or 10. But may contain other codes.

If the DemoResponse "response" contains the code 2 as the first Code, then this line works fine:

int myCode = int.Parse(response.Errors.Select(x => x.StackTrace).FirstOrDefault());

But if the DemoResponse "response" does not have code 2 as first, then how do I write a LINQ to select Code 2 and assign to "myCode" variable? For example:

  "StackTrace" : "95",
  "StackTrace" : "95",
  "StackTrace" : "2"

CodePudding user response:

Need to add for each loop inside if condition.

private const string Codes = "2,10";

public void method()
{
    var displayCodes = Codes.Split(',');
    DemoResponse response = webService.GetResponse();  //getting response from API

    if(response.Errors.Any(x => displayCodes.Contains(x.StackTrace))
    {
        foreach (ErrorResponse item in response.Errors)
        {
          // write your logic based on your requirement

          if(int.Parse(item.StackTrace) == 2)
          {
          int myCode = int.Parse(item.StackTrace);
          }
        }
    }
}

CodePudding user response:

It seems to me that what you want to receive form the API is an array of information, but in your code you are mapping what you receive from the API into a single object of type DemoResponse. I suspect that you actually want response to be a list or array of DemoResponses.

Furthermore, from my understanding of the following question:

But if the DemoResponse "response" does not have code 2 as first, then how do I write a LINQ to select Code 2 and assign to "myCode" variable?

, you want to assign the first StackTrace value that matches any of the codes in Codes, to myCode; rather than assigning any StackTrace value to myCode.

If those two assumptions are correct, you could try the following:

private const string Codes = "2,10";

public void method()
{
    var displayCodes = Codes.Split(',');

    // NOTE: response is now an array of DemoResponse; 
    // GetResponse()'s implementation and return type may need to be rewritten accordingly
    DemoResponse[] response = webService.GetResponse();

    var errors = response.SelectMany(r => r.Errors);
    
    if (errors.Any(x => displayCodes.Contains(x.StackTrace)))
    {
        int myCode = int.Parse(errors.Select(x => x.StackTrace).First(s => displayCodes.Contains(s)));
    }
}

Alternatively, you could look loop through the stack traces rather than the errors:

var stackTraces = response.SelectMany(r => r.Errors).Select(e => e.StackTrace);
    
if (stackTraces.Any(s => displayCodes.Contains(s)))
{
    int myCode = int.Parse(stackTraces.First(s => displayCodes.Contains(s)));
}
  • Related