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 476031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:26:53+00:00 2026-05-13T00:26:53+00:00

So I have this LINQ query that ends in a custom select kinda like

  • 0

So I have this LINQ query that ends in a custom select kinda like this:

select new { this1 = table.this1, this2 = othertable.this2 }

The call to that query from the Controller looks something like this:

ViewData["these"] = theRepo.GetAllThese(someVar, anotherVar);

Now when I pass this on to my view since it is not strongly typed how can I iterate through it with a foreach, how can I cast it as an IQueryable or a List if I don’t know what’s in it?

…is it something like this?

IQueryable<???> these = ViewData["These"];
foreach (var this in these) {...

Just need to know what to put for ‘???’ I think.

  • 1 1 Answer
  • 1 View
  • 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-13T00:26:54+00:00Added an answer on May 13, 2026 at 12:26 am

    Your linq query returns a collection of anonymously typed objects. Being anonymous, there is no way to “call their name” when declaring an explicitly typed variable. Thus, the true type/shape of the objects is only known within the action method where the objects are defined.

    The indexed getter of the ViewData object has a return type of object, and without knowing the type name, you want be able to cast the return value of ViewData["these"] to anything useful.

    What you might want to do instead, is to create a model – more specifically a “view model” – which defines the structure of the objects you are selecting using LINQ:

    public class FoobarViewModel
    {
        public string Foo { get; set; }
        public string Bar { get; set; }
    }
    

    and redefine your query to do a select like this:

    select new FoobarViewModel { foo = table.this1, bar = othertable.this2 }
    

    Your objects now share a common named class, and your collection can be easily casted to the proper type in the view.

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

Sidebar

Related Questions

No related questions found

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.