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

6

7

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.

Visser answered 31/12, 2020 at 10:38 Comment(1)
This just happened to me when I tried to use a callback of type IEnumerable<>. Solved by changing to List<>.Parthia
V
8

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 ?

Visser answered 3/1, 2021 at 9:11 Comment(0)
C
12

Happened to me twice.

One time was due to me deleting a method from the @code at the bottom, but then forgetting to delete the button that calls the method.

Another time was due to me trying to access Properties/Field that doesn't exist.

In both cases, Visual Studio did not redline the issue, and also the line number reported in compilation error message does not correspond to where the code actually has issue. But one good news is that Intellisense autocomplete is correct. If you type your code, it wouldn't let you autocomplete with variable or method that doesn't exist.

You can try to get a more accurate error from Output window by searching for "error". In image below, the text in the Output window is the correct issue while the text in the "Error List" window isn't helpful. Also, when I click the error text in the Output window, it took me to the correct line in my code that has the issue.

Error List window vs Output window

Creath answered 25/7, 2022 at 23:52 Comment(2)
Good anwser actaully tells me how to find the problem regardless of what it isGuillory
This did it for me, thanks: "You can try to get a more accurate error from Output window by searching for "error"."Misrepresent
V
8

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 ?

Visser answered 3/1, 2021 at 9:11 Comment(0)
E
3

This answer may help others with the same error. Thank you @Hassan Gulzar for posting your "fix"

Your words: These errors are on the .g.cs file of the page. was exactly my situation that got me to look at my code in the HTML that was causing the same ERROR you fixed.

In my case with Blazor-WASM, .Net 6, using an <InputSelect...> control, I changed the HTML-control to <select...> and the subject-error got fixed. Your solution got me directly to fixing my code. Thank you very much.

Enterovirus answered 16/3, 2022 at 13:9 Comment(0)
B
1

In my case I had changed the model class of an EditForm to:

NewRegForm newRegistration = new NewRegForm();

Then I had a reset button with a function trying new the old type.

@onclick="@(() => newRegistration = new Registration())"
Burdensome answered 18/5, 2022 at 2:42 Comment(0)
W
0

Thank you for posting your fix, I actually look into this error for long time and finds that this ".g.cs" is not even exist So the error of such type is just because your syntax error like your's In my case I have added one function onclick in code but had already deleted that function definition and even there is no red marking over it neither error of no function exist of such name.

Winfield answered 20/7, 2022 at 13:50 Comment(0)
N
0

I ran into this issue in a @onclick handler in a component where I passed an EventCallback as a parameter.

The key was to not simply call with await function(value). I had to use await function.InvokeAsync(value). Problem solved.

Not exactly your situation, but documenting my fix here :)

The code that didn't work:

[Parameter] public EventCallback<long> MyFunction { get; set; }

<span @onclick="@(async () => await MyFunction(currentData.UserObservationId))"></span>

The fix, using the same parameter:

<span @onclick="@(async () => await MyFunction.InvokeAsync(currentData.id))"></span>

Note my code was inside of a Syncfusion template, so currentData.id could be whatever you need.

Nedry answered 19/6, 2024 at 20:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.