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

  • Home
  • SEARCH
  • 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 8492325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:41:09+00:00 2026-06-10T22:41:09+00:00

i have this LINQ query on two tables public ViewResult data() { var query

  • 0

i have this LINQ query on two tables

public ViewResult data()
        {
            var query = from a in DB.Album
                        join b in DB.Artists
                        on a.ArtistId equals b.ArtistId
                        where (b.ArtistId == 2)
                       select new { a ,b };

            return View(query.ToList());
        }

however i am confused how do i display these data i.e Artist table’s Artist name and Album tables Album Name in my view?

i am trying something like this but it gives me a error

@model IEnumerable<test1.Models.Album>


@{
    ViewBag.Title = "data";
}

<h2>data</h2>

@Model.Count()
<br />
<ul>
@foreach (var mod1 in Model)
{
    <li>

   @* @mod1.Genre.Name*@
      @Html.DisplayFor(modelItem => mod1.Artist.Name)
    <br />
    @mod1.AlbumId
   <br />
    @mod1.Artist
    <br />
    @mod1.Title
    </li>
}
</ul>

Error i get is

The model item passed into the dictionary is of type ‘System.Collections.Generic.List1[<>f__AnonymousType1
2[test1.Models.Album,test1.Models.Artist]]’,
but this dictionary requires a model item of type ‘System.Collections.Generic.IEnumerable`1[test1.Models.Album]’.

Any idea ?

  • 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-06-10T22:41:11+00:00Added an answer on June 10, 2026 at 10:41 pm

    You are getting the exception because you are trying to pass a list of anonymous objects
    instead of a list of albums.

    You can create a viewmodel so you can use that type as your model.

    public class ArtistAlbumViewModel
    {
        Artist Artist { get; set; }
        Album Album { get; set; }
    }
    

    Use this type when you return values from the query.

    var query = from a in DB.Album
                join b in DB.Artists
                on a.ArtistId equals b.ArtistId
                where (b.ArtistId == 2)
                select new ArtistAlbumViewModel { Album = a, Artist = b };
    

    Then you should strongly type your view to this type and edit the view according to your viewmodel.

    @model IEnumerable<test1.Models.ArtistAlbumViewModel>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this following IEnumerable LINQ query: var query = from p in Enumerable.Range(2,
I have a Linq query that looks something like this: var myPosse = from
I have a Linq query that looks something like this: var query = from
i have the following linq query that create a left join between two tables:
So I have this linq query clause. var ctx = new Context(); IQueryable<Users> consulta
I have a LINQ query that returns some object like this... var query =
I have a simple linq query that unionizes two tables that implement the same
I have this linq query that works well (although it may be written better,
I need some help with a LINQ query in VB.Net, please. I have this
I'm having an issue getting a LINQ query to work. I have this XML:

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.