Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
The problem is that topAgents
is dynamic
- so your ToList()
call is dynamic, and so is Select
. That has issues that:
- you can't use lambda expressions for dynamic calls like this;
- dynamic calls don't find extension methods anyway.
Fortunately, the operations don't need to be dynamic just because the element type is dynamic. You could use:
IEnumerable<dynamic> topAgents = ...;
... or just use var
. Both of those should be fine.
Cannot use a lambda expression as an argument to a dynamically dispatched operation Kendo ListView
@(Html.Kendo().ListView<Entity.Models.DocViewModel>()
.Name("listView")
.TagName("div")
.ClientTemplateId("DocumentList")
)
Nevermind, I had a syntax error.
Cannot use a lambda expression as an argument to a dynamically dispatched
The error message is pretty clear I think.
An anonymous lambda can represent a delegate (and there can be many matching delegate types) or an expression tree. It does not have a type by itself until assigned to a variable of specific type or, used in a context where a specific type is expected. When regular (non-dynamic) types are used the compiler can usually deduce target type (such as Func<string, string>
). When dynamic types are involved though - compiler cannot do that, because all resolutions are now performed at runtime, not at compile time. So compiler will not assign type Func<string, dynamic, dynamic>
to your lambda and you should do it yourself (as compiler suggests):
cache.AddOrUpdate(key, value, (Func<string, dynamic, dynamic>) ((k, v) => value));
Example here.
error CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation
Using a foreach
and creating a parameterized SqlCommand
is best practice
var ds = (JArray)o["Tables"][0]["Rows"];
using (var connection = new SqlConnection(cnnString)) {
connection.Open();
var cmdIns = new SqlCommand("INSERT INTO dbo.AddPlay(UserId, Timestamp, YoutubeId, Source, PlayCount, Rating) VALUES(@UserId, @Timestamp, @YoutubeId, @Source, @PlayCount, @Rating)", connection);
cmdIns.Parameters.Add("@UserId", SqlDbType.VarChar, 20);
cmdIns.Parameters.Add("@Timestamp", SqlDbType.VarChar, 20);
cmdIns.Parameters.Add("@YoutubeId", SqlDbType.VarChar, 20);
cmdIns.Parameters.Add("@Source", SqlDbType.VarChar, 20);
cmdIns.Parameters.Add("@PlayCount", SqlDbType.Int);
cmdIns.Parameters.Add("@Rating", SqlDbType.Int);
foreach (var ja in ds) {
cmdIns.Parameters["@UserId"].Value = ja[0].Value<string>();
cmdIns.Parameters["@Timestamp"].Value = ja[1].Value<string>();
cmdIns.Parameters["@YoutubeId"].Value = ja[2].Value<string>();
cmdIns.Parameters["@Source"].Value = ja[3].Value<string>();
cmdIns.Parameters["@PlayCount"].Value = GetInt(ja[4]);
cmdIns.Parameters["@Rating"].Value = GetInt(ja[5]);
cmdIns.ExecuteNonQuery();
}
}
ASP.NET MVC: Cannot use a lambda expression as an argument to a dynamically dispatched operation
It looks like you're doing this in your view, which violates the principles of separation of concerns. But this is how you would do it.
@
{
var layers = Model.layers.Where(x => x.KONT == "EUROPE").ToList();
}
@foreach(var layer in layers)
{
.....
}
A Better Way
What you should do however is create a method on your Model "GetLayersForLocation" Then your code would look like this:
In Your Model Class
public IEnumerable<Layer> GetLayersForLocation(string location)
{
return this.layers.Where(x => x.Knot == location);
}
In Your View Code
@foreach(var layer in Model.GetLayersForLocation("EUROPE"))
{
.....
}
The reason this is better is you can now unit test your code, before you wouldn't be able to because it's just part of your view, but now you can run automated tests to ensure that getting the proper layers is working.
MatchEvaluator gives Cannot use a lambda expression... error
You are replacing the contents of DBText. Regex.replace expectes a string as first argument. I am guessing DBText is of type dynamic, which confuses the compiler in this particular case.
You can simply cast DbText to string and you should be fine
patternURL.Replace((string)DBText, (...)
Error passing both a dynamic object and an Action to a function
Sure - do exactly as the compiler says - cast the lambda expression to a concrete type:
Calc(obj, (Action<int>)(result => Console.Write("Result: " + result)));
The reason you have to do this is that a lambda expression doesn't have a type - the compiler has to know what delegate (or expression tree) type you're trying to convert it to. It can't do that if the method you'll be calling won't be chosen until execution time, which is the case when another argument is dynamic.
Related Topics
How to Use Dependency Injection in a .Net Core Actionfilterattribute
Using System.Io.Packaging to Generate a Zip File
How to Get Character for a Given Ascii Value
The 'Await' Operator Can Only Be Used Within an Async Lambda Expression
How to Pass Objects into an Attribute Constructor
How to Generate and Auto Increment Id with Entity Framework
Comparing 2 Objects and Retrieve a List of Fields with Different Values
Equality Comparison Between Multiple Variables
Get the Decimal Part from a Double
How to Programmatically Create, Read, Write an Excel Without Having Office Installed
Calling SQL Defined Function in C#
Using Folderbrowserdialog in Wpf Application
Cannot Deserialize JSON Array into Type - JSON.Net
Change Color of Text Within a Winforms Richtextbox
C# - How to Copy a Single Excel Worksheet from One Workbook to Another