Search code examples
blazor-server-side.net-5

Cannot convert lambda expression to intended delegate type because some of return types in block aren't implicitly convertible to delegate ret-type


I have this code on a Blazor Server-Side page:

protected override async Task OnInitializedAsync()
{
    try
    {
        _model = await BudgetReleaseRequestProviderService.GetMeetingById(MeetingId);           

        var meetingRequestsIds = await BudgetReleaseRequestProviderService.GetMeetingDetailsForMeeting(_model.Id);
        var selectedRequestIds = meetingRequestsIds.Select(s => s.RequestId).ToArray();

        var meetingRequests = (await BudgetReleaseRequestProviderService.GetAllRequests())
            .Where(w => selectedRequestIds.Contains(w.Id)).ToList();

        _requestsData = new List<MeetingRequestActionViewModel>(meetingRequests.Cast<MeetingRequestActionViewModel>());
    }
    catch (Exception e)
    {
        LoadingStatus = $"Error while loading meeting #{MeetingId}. Error: {e.Message}";
        Console.WriteLine(e);
    }
}

There is no error but when I build, I get these errors:

Errors:

  • CS1662 Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
  • CS0131 The left-hand side of an assignment must be a variable, property or indexer

These errors are on the .g.cs file of the page. I cannot amke sense of that file. I have a Contains call in my Linq and that is the closest I have reached to troubleshooting.

Classes:

public class MeetingRequestActionViewModel: BudgetReleaseRequestViewModel
    {}

public partial class BudgetReleaseRequestViewModel
{       
    public long Id { get; set; }

    [Required]
    [Display(Name = "Description")]
    public string Description { get; set; }

    [Required]
    [Display(Name = "Amount")]
    [DataType(DataType.Currency)]
    public decimal Amount { get; set; }

    public string RequesterId { get; set; }

    [Display(Name = "Budget Owner")]
    public string BudgetOwnerId { get; set; }

    [Required]
    [Display(Name = "Submission Date")]
    [DateTodayAndOnwards(ErrorMessage = "This date cannot be before today.")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "dd/MMM/yyyy")]
    public DateTime SubmissionDate { get; set; }
}

For the sake of this page only, I decided to create a view model based on an existing one and add the two extrs required fields. That's why there is a converstion.


Solution

  • Turns out the error was pointing to a razor syntax and not my code block. Here was the original line(s) causing grief:

    <DxDataGridColumn Field="@nameof(MeetingRequestActionViewModel.MeetingResultComments)">
        <DisplayTemplate Context="currentRequest">
            @{
                var thisItem = currentRequest as MeetingRequestActionViewModel;
               <TelerikEditor @ref="@_editorRef" @bind-Value="@thisItem?.MeetingResultComments" Height="200px" Tools="@EditorToolSets.Default">
               </TelerikEditor>
            }
        </DisplayTemplate>
    </DxDataGridColumn>
    

    and here is my modification:

    <DxDataGridColumn Field="@nameof(MeetingRequestActionViewModel.MeetingResultComments)">
        <DisplayTemplate Context="currentRequest">
            @{
                if (currentRequest is MeetingRequestActionViewModel thisItem)
                {
                    <TelerikEditor @ref="@_editorRef" @bind-Value="@thisItem.MeetingResultComments" Height="200px" Tools="@EditorToolSets.Default">
    
                    </TelerikEditor>
                }
            }
        </DisplayTemplate>
    </DxDataGridColumn>
    

    My original code was causing issue as I was indirectly converting the thisItem?.MeetingResultComments to a Nullable with the ?