2

I'm trying to perform the following

int myObject = getValues(someVar).Sum(x => Int32.Parse(x.Price))

The function looks this:

List<dynamic> getValues(string something) {...}

This is the error I'm receiving: "Cannot use a lambda expression as an argument to a dynamically dispatched operation"

How can I SUM the values of a List object in a chained call similar to LINQ SUM?

user1234
  • 57
  • 1
  • 1
  • 8

4 Answers4

4

Your code works. The problem you are having isn't in the code you posted. This code runs.

void Main() {
    int myObject = getValues("12").Sum(x => Int32.Parse(x.Price));
    Console.WriteLine (myObject);
}

List<dynamic> getValues(string something) {
    var item = new { Price = something };
    IEnumerable<dynamic> items = Enumerable.Repeat<dynamic>(item, 2);
    return items.ToList();
}

This produces the output 24. The problem may be related to type inference, but that's just a guess. You should include enough code to reproduce the error for a more reliable answer.

recursive
  • 83,943
  • 34
  • 151
  • 241
  • odd my code is setup almost identically but I get an error " "CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type". This is a webforms project / .net 4.5 – user1234 Apr 20 '15 at 17:35
  • 1
    They only lambda expression in an argument in your posted code is the call to `.Sum()`. But I don't think that's dynamically dispatched. In order for anyone to help you, you'll need to provide enough information to reproduce the problem. Take your code out of it's context in your application, and reduce it to the minimum required to create the error. As it is, we're just guessing. – recursive Apr 20 '15 at 17:41
1

As mentioned in the comment, this code is working fine for me:-

public static void Main()
{
     var result = GetData("test").Sum(x => int.Parse(x.Name));
     Console.WriteLine(result);
}

    public static List<dynamic> GetData(string x)
     {
         List<dynamic> data = new List<dynamic>
         {
             new { Id =1, Name ="1"},
             new { Id =2, Name ="4"},
             new { Id =3, Name ="5"}
         };
         return data;
     }

I am getting 10 as output.

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
  • odd my code is setup almost identically but I get an error " "CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type". This is a webforms project / .net 4.5 – user1234 Apr 20 '15 at 17:34
  • 1
    and please don't spam comments like that - you can always edit your question – Random Dev Apr 20 '15 at 17:36
  • @Rahul Singh, I guess this code's [fiddle](https://dotnetfiddle.net/O22wlB) throws an exception because - as explained [here](http://stackoverflow.com/a/7104323/3002584) - anonymous types are created as `internal` by the compiler, so probably some DotNetFiddle assembly can't access them. If one would like to nevertheless use this fiddle, he would have to use some really ugly Reflection tricks (I added some other usages): [https://dotnetfiddle.net/CX0HtS](https://dotnetfiddle.net/CX0HtS). – OfirD Feb 27 '17 at 18:36
1

so it ultimately turned out the issue was that I was passing a dynamic variable into the function call and subsequently using LINQ/lambda. Seems like that's a compiler no-no...

dynamic someVar = new {a=1,b=2};

int myObject = getValues(someVar.a).Sum(x => Int32.Parse(x.Price))
user1234
  • 57
  • 1
  • 1
  • 8
  • If you replace `var someVar` with `dynamic someVar`, then this would make sense as an explanation. As it stands though, `var` wouldn't be enough to explain the compiler error. – recursive Apr 20 '15 at 18:01
  • 1
    but still this don't seem to be an answer - why not just add it to your question instead? – Random Dev Apr 20 '15 at 18:07
  • 2
    My honest advice to you is: 1. don't use dynamic - C# has a good enough type-system that you should rarely need it (surely not in this case). 2. Use the debugger 3. read the FAQ/Help around here on how to ask questions – Random Dev Apr 20 '15 at 18:12
0

Your getValues method is returning a dynamic, not a List<dynamic>. Either change your method signature or construct a new List<dynamic>(...) from its results.

dynamic list = new [] {new{Price = "1"},new{Price = "2"}};

// This produces the error you're describing:
Console.WriteLine(list.Sum(x => Int32.Parse(x.Price)));

// This works.
Console.WriteLine(new List<dynamic>(list).Sum(x => Int32.Parse(x.Price)));
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • But return type looks List to me:- `List getValues` – Rahul Singh Apr 20 '15 at 17:20
  • @RahulSingh: Yes, in the OP it appears that way, but you're assuming that the OP correctly posted his code as-is. I'm reading between the lines, and I'm pretty sure I'm right on this one. – StriplingWarrior Apr 20 '15 at 17:26
  • 1
    I am not the OP :D nor I am the down-voter, good analysis though. – Rahul Singh Apr 20 '15 at 17:26
  • it is return List. oddly i get the error "CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type" – user1234 Apr 20 '15 at 17:30
  • 3
    @user1234 please post your exact code without editing so we can check – Random Dev Apr 20 '15 at 17:36