1

I have a API method on a .net 7.0 API. I have the following method for completing some batch operations and on the return need to rely a number of success/failures;

    public async Task<IActionResult> BatchFail([FromBody] JobBatchSignatureModel model)
    {
        var results = new List<(string JobId, string Message, bool HasError)>();

        foreach (var job in model.Jobs)
        {
            try
            {
                /*Removed for Brevity*/

                results.Add((JobId: job.JobId.ToString(), Message: "Success", HasError: false));
            }
            catch (Exception ex)
            {
                results.Add((JobId: job.JobId.ToString(), Message: ex.Message, HasError: true));
            }
        }

        if (results.Any(x => x.HasError == true))
        {
            return BadRequest(results);
        }
        else
        {
            return Ok(results);
        }
    }

As the output I am expecting is:

[
    {
        "JobId": "13",
        "Message": "User does not match the one trying to start the job (Parameter 'userId')",
        "IsError": true
    }
]

however I get:

[
    {
        "Item1": "13",
        "Item2": "User does not match the one trying to start the job (Parameter 'userId')",
        "Item3": true
    }
]

How can I resolve this please without putting concrete classes in place?

Matthew Flynn
  • 3,661
  • 7
  • 40
  • 98
  • 5
    There are a *lot* of duplicate questions. A named tuple is simply compiler magic that allows using a name to refer to `Item1`, `Item2`, etc. Tuples aren't appropriate as return types of APIs, either web APIs or libraries. Use `record` or `record struct` to define a new type with new properties – Panagiotis Kanavos May 24 '23 at 09:41
  • See: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples#tuple-field-names for details. – urbanSoft May 24 '23 at 09:46

0 Answers0