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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:17:44+00:00 2026-05-12T11:17:44+00:00

I have a simple data model of two tables, email and recipients, email can

  • 0

I have a simple data model of two tables, email and recipients, email can be sent to one or more recipients

I have setup the database with the two tables, created the Linq to SQL repository, built the controllers and the strongly typed view.

This works fine when I want to select all records from the database

public IList<AllMailDetail> ListAll()
    {
        var allMail =
            from m in _datacontext.mail_receiveds
      join r in _datacontext.mail_recipients on m.DeliveryId equals r.DeliveryId
            select new AllMailDetail {
                                        DeliveryId = m.DeliveryId,
                                        MessageId = m.MessageId,
                                        SentFrom = m.SentFrom,
                                        FilePath = m.FilePath,
                                        FileName = m.FileName,
                                        SentDateTime = m.SentDateTime,
                                        ReceivedDateTime = m.ReceivedDateTime,
                                        Subject = m.Subject,
                                        SpamScore = m.SpamScore,
                                        IsSpam = m.IsSpam,
                                        SenderIP = m.SenderIP,
                                        Header = m.Header,
                                        SentTo = r.SentTo
                                      };

        return allMail.ToList <AllMailDetail>();
    }

The custom type class

public class AllMailDetail
{
    public int DeliveryId { get; set; }
    public int? MessageId { get; set; }
    public string SentFrom { get; set; }
    public string FilePath { get; set; }
    public string FileName { get; set; }
    public string SentDateTime { get; set; }
    public DateTime ReceivedDateTime { get; set; }
    public string Subject { get; set; }
    public byte? SpamScore { get; set; }
    public bool? IsSpam { get; set; }
    public string SenderIP { get; set; }
    public string Header { get; set; }
    public string SentTo { get; set; }
}

The controller simply sends the contents from the repository to the strongly typed view

public ActionResult Index()
    {
        return View(_repository.ListAll());
    }

To get just one mail record from the database I have the following code that accepts a deliveryId

public IQueryable<AllMailDetail> GetMail(int? id)
    {
        var allMail =
            from m in _datacontext.mail_receiveds
            join r in _datacontext.mail_recipients 
            on m.DeliveryId equals r.DeliveryId
            where m.DeliveryId == id
            select new AllMailDetail
            {
                DeliveryId = m.DeliveryId,
                MessageId = m.MessageId,
                SentFrom = m.SentFrom,
                FilePath = m.FilePath,
                FileName = m.FileName,
                SentDateTime = m.SentDateTime,
                ReceivedDateTime = m.ReceivedDateTime,
                Subject = m.Subject,
                SpamScore = m.SpamScore,
                IsSpam = m.IsSpam,
                SenderIP = m.SenderIP,
                Header = m.Header,
                SentTo = r.SentTo
            };

        return allMail;
    }

And its controller code

public ActionResult Details(int? id)
    {       
        var mail = _repository.GetMail(id);

        if (mail == null)
            return View("NotFound");

        return View(mail);
    }

I had been trying to display the output for a single record by also using a strongly typed view having Inherits=”System.Web.Mvc.ViewPage At the top of the aspx page but I got the following error

The model item passed into the dictionary is of type ‘System.Data.Linq.DataQuery`1[projectMail.Models.AllMailDetail]’ but this dictionary requires a model item of type projectMail.Models.AllMailDetail’.

I fixed this error after much searching and found this post most helpful
MVC LINQ to SQL Table Join Record Display

so my view is no longer strongly typed and I build the page as follows

<% foreach (projectMail.Models.AllMailDetail item in (IEnumerable)ViewData.Model)
    { %>

       ...items...

 <%  } %>

This works fine, but it seems the long way round. The thing I can’t figure out is

  1. Why does the second query need to be IQueryable
  2. Why didn’t it work when the view was strongly typed
  3. How can it be made to work with a strongly typed view
  4. Is this the best way of dealing with joins in MVC using LINQ to SQL
  • 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-12T11:17:45+00:00Added an answer on May 12, 2026 at 11:17 am

    Hmmmm, Try in the controller

    return View(_repository.GetMail( id).SingleOrDefault());
    

    You’re trying to bind an IQueryable datasource to an AllMailDetail View, the above should fix you.

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

Sidebar

Related Questions

I'm using core data and I have a simple two-entity model. My navigation controller
I'm new to WCF data services. I have a quite simple data model. Some
I have fit a simple bi-variate VAR model to this data set and I
I have a simple jasper report which shows some data from database. The jasper
I use c#, linq and EF4. I have two tables in my DataBase represented
I have a pretty simple console project with an entity model (containing two simple
I am working on an application with a kinda simple data model (4 tables
So I have two separate models, one 'items' model, the second a 'sites' model...
I have only simple data types in method signature of service (such as int,
Let's say I have some simple data y = [[datetime.datetime( 2012,1,1,1,1), 2.1], [datetime.datetime( 2012,1,1,1,2),

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.