Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run linq lambdas inside template seems to have problems #127

Open
Shay-da opened this issue Jan 18, 2023 · 2 comments
Open

run linq lambdas inside template seems to have problems #127

Shay-da opened this issue Jan 18, 2023 · 2 comments

Comments

@Shay-da
Copy link

Shay-da commented Jan 18, 2023

Hey :) I need to update our old razorEngine to a new one- while most of our templats run fine- we have a few template which are a bit more complex. when running template with the following code-

foreach (var field in Model.Fields.Where(f => !String.IsNullOrEmpty(f.Label) && f.Label.Equals("Some Text", StringComparison.OrdinalIgnoreCase)))

this worked fine with the old razor engine..
with the new one we get-the following error:

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.

so after pulling the enumarator logic and assign it outside (in our example lets say - var fields = Model.Fields...) the linq query we faced another error- 'System.Collections.Generic.List' does not contain a definition for 'FirstOrDefault' although we added
@using System.Linq in the cshtml file and also while creating the template we inject System.Core, Version=4.0.0.0 assembly to make sure its not dll issues.. any idea if we are in the right direction or im missing something?

@adoconnection
Copy link
Owner

Hi! If fields is a dynamic property you need to cast it to List/Enumerable first
see TestCompileAndRun_DynamicModelLinq unit test

public void TestCompileAndRun_DynamicModelLinq()

@GreenIreland
Copy link

Hi! I got same issue with lambdas.

Razor code:
@{ var wallet = (IEnumerable<object>)(Model.Guest.UserWallets).Where(uw => uw.Wallet.IsActive).Single(); }

UserWallets is IList property.
Wallet.IsActive is boolean property.

Result:
CS1977 error - Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.

@{ var wallet = (IEnumerable<object>)(Model.Guest.UserWallets).Single(); } works well.

@adoconnection Any ideas how to fix this / how to use lambda expressions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants