Ternary function - Type of expression cannot be determined.... conversion between <ActionResult> and Task<String>

Cloud Developer 21 Reputation points
2024-03-05T15:13:24.52+00:00

I'm using an ternary expression in my code, LINQ Query statement as such:

Type of conditional expression cannot be determined because there is no implicit conversion

between 'Microsoft.AspNetCore.Mvc.ActionResults<System.Collections.Generic.IEnumberable<string> and System.Threading.Task.Task<string>

Is there another way to accomplish the same thing?

so, if the string coming back from the table begins with X (yes I have contains, but it can start with ( X, XX, XXX, or even XXXX), I need to parse it out, remove the X and split it by { ; }, if it does not, then it'll call another function, do some formatting and show in the UI.

public sync Task<ActionResult> OnGetModels(int modelId)
{
      var output = await _db.CarModels
          .Where(x=> x.id == modelId)
          .Select(x => new 
           {
               carDetails = x.Details.Contains("X") ? ParseOutput(x.Details) : ShowDetails(x.Details)
            }).ToArrayAsync();

       return new JsonResults(output); 
      
}

// I get the following error when calling this:
// Type of conditional expression cannot be determined because there is no implicit conversion
//between 'Microsoft.AspNetCore.Mvc.ActionResults and System.Threading.Task.Task<string>
public static ActionResults<IEnumberable<string>> ParseOutput(string outputValues)
{
       //here is where I parse the string to remove the leading X's and split by { ; } 
       // and return for the OnGetModels to return the output to the UI

}


Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2024-03-06T07:30:51.96+00:00

    Hi @Cloud Developer ,Welcome to Microsoft Q&A,

    If there are only two possible categories, you can also use try catch to parse both strings, and then perform another processing method on failure. This ensures that no matter what the input string is, it will be processed.

    public async Task<ActionResult> OnGetModels(int modelId)
    {
         var carModel = await _db.CarModels.FirstOrDefaultAsync(x => x.id == modelId);
         if (carModel != null)
         {
             IEnumerable<string> output;
             try
             {
                 output = ParseOutput(carModel.Details);
             }
             catch
             {
                 output = ShowDetails(carModel.Details);
             }
    
             return new JsonResult(output);
         }
         else
         {
             return NotFound();
         }
    }
    
    public IEnumerable<string> ParseOutput(string outputValues)
    {
         // Try to parse the string here, if the parsing fails, throw an exception
         // Otherwise, remove the leading X and split into { ; }, then return the result
    }
    
    public IEnumerable<string> ShowDetails(string outputValues)
    {
         // Perform other formatting or processing here and return the result
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-03-15T20:40:28.3533333+00:00

    you just want the ActionResult value. also as sql cannot execute an c# code, do the query first.

     var output = (await _db.CarModels
                     .Where(x=> x.id == modelId)
                     .ToListAsync()
              )
              .Select(x => new 
               {
                   carDetails = x.Details.Contains("X") 
                        ? ParseOutput(x.Details).Value
                        : ShowDetails(x.Details).Value
               }).ToArray();
    

    this assumes ShowDetails() is also ActionResult<IEnumberable<string>>


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.