Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6011507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:16:12+00:00 2026-05-23T02:16:12+00:00

I am trying to mock up a page using WebMatrix using WebPages under the

  • 0

I am trying to mock up a page using WebMatrix using WebPages under the hood. I have assigned an implicitly typed array of anonymous objects to one of the PageData keys, but I get the following error when I try to using LINQ methods on the collection:

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

Here is some sample code:

@{
  PageData["Vals"] = new [] {
    new { ID=1, Quantity=5 },
    new { ID=2, Quantity=3 }
  };
  var sum = PageData["Vals"].Sum(x => x.Quantity);
}

If I first store the array in a regular object, I can use the LINQ methods on it just fine. It seems to have a problem when it comes out of PageData as a dynamic object – but I can’t quite seem to figure out the secret sauce to coerce it back to the initial type.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:16:13+00:00Added an answer on May 23, 2026 at 2:16 am

    The general solution to this problem is to explicitly cast it. i.e. Cast the expression PageData["Vals"] to an array of the type you expect. However, this cannot work with anonymous types because you don’t have the handle to its type and therefore cannot cast it.

    Once you’ve stored your new[] { ... } in the dynamically typed PageData, you’ve lost all compile-time reference to the anonymous type. Therefore, trying to use type-specific LINQ operators on it is a non-starter.

    As I mentioned in the comments, the correct solution is to always use strongly-typed models. You should not be relying on anonymous types declared and defined within a view in order to mock up the page. Have the page depend on a real model and populate that model and feed it to the page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to mock java.awt.Toolkit.beep() using JMockit Expectations. I have the following code
I have an object I'm trying to mock using moq. The object's constructor has
I've been trying to get to mock a method with vararg parameters using Mockito:
I´m encountering this problem trying to mock some objects that receive complex lambda expressions
I am trying to mock a class using Mockito and also using PowerMock. This
I'm currently trying to mock the following method using Jack. The code example is
I'm trying to mock out the System.net.Sockets.Socket class in C# - I tried using
I am trying to mock database operations. I have problem in mocking SqlParameterCollection .
I have a question related to jmock library. I am trying to mock a
I am using Google Mock 1.6 RC and am trying to Mock a COM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.