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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:13:34+00:00 2026-06-17T09:13:34+00:00

I am trying to create a ViewModel for data that I want to display

  • 0

I am trying to create a ViewModel for data that I want to display in my view. The issue is, the data being displayed is spread across 2 databases and multiple tables within each. I’ve read that you cannot join Linq queries across multiple contexts, which makes sense, and I’ve also read that we can’t use Code-First in EF5 to use Stored Procedures….which led me to using 3 different Linq queries and attempt to combine them into 1 ViewModel…I’m just not sure how to get there.

Here’s my Linq queries:

var csdContext = new CSDContext(CustomerCode);
var masterContext = new MasterContext();

//Only returns 1 row - which is what we want.
List<Site> sites = (from s in csdContext.Sites
                join sa in csdContext.SiteAddresses
                    on s.SiteID equals sa.SiteID
                join a in csdContext.Addresses
                    on sa.AddressID equals a.AddressID
                join spv in csdContext.SiteProductVersions
                    on s.SiteID equals spv.ProductVersionID
                where s.SiteID == id
                select s).ToList();

//List
List<States> states = (from s in masterContext.StatesTable
                    select s).ToList();

My ViewModel looks like this:

public class SiteDetailsViewModel
{
    public string Address { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string StateCode { get; set; }
    public string ZipCode { get; set; }
    public string OfficePhone { get; set; }
    public string MobilePhone { get; set; }
    public string AlternativePhone { get; set; }
    public int ProductVersionID { get; set; }
}

Basically, I need the following data from these tables:
csdContext – Address
Address
Address2
City
ZipCode

csdContext – Sites
OfficePhone
MobilePhone
AlternativePhone

csdContext – SiteProductVersions
ProductVersionID

masterContext – States
StateCode

Here’s how the tables are joined in SQL:

SELECT csd_a.Address, csd_a.Address2, csd_a.City, mstr_st.StateCode, csd_a.ZipCode, csd_s.OfficePhone, csd_s.MobilePhone, 
csd_s.AlternativePhone, csd_spv.ProductVersionID
FROM CSD.dbo.Sites AS csd_s 
INNER JOIN  CSD.dbo.SiteAddress AS csd_sa ON csd_sa.SiteID = csd_s.SiteID 
INNER JOIN  CSD.dbo.Address AS csd_a ON csd_a.AddressID = csd_sa.AddressID 
INNER JOIN CSD.dbo.SiteProductVersions AS csd_spv ON csd_s.SiteID = csd_spv.SiteID 
INNER JOIN MasterDB.dbo.States AS mstr_st ON mstr_st.StateID = csd_a.StateID 

I can’t figure out how to merge these 3 results to create the ViewModel data for SiteDetailsViewModel. Can anyone help?

  • 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-17T09:13:35+00:00Added an answer on June 17, 2026 at 9:13 am

    If you materialize your queries as described in OP you can use this:

    var query = sites.Join(
                states,
                si => si.StateID,
                st => st.StateID,
               (si, st) => new SiteDetailsViewModel
                            {
                                 Address = si.Address,
                                 Address2 = si.Address2,
                                 City = si.City,
                                 StateCode = st.StateCode,
                                 ZipCode = si.ZipCode,
                                 OfficePhone = si.OfficePhone,
                                 MobilePhone = si.MobilePhone,
                                 AlternativePhone = si.AlternativePhone,
                                 ProductVersionID = siProductVersionID
                            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an HtmlHelper extension that outputs some HTML to the view.
Trying to create a black line in my view to separate text blocks but
Trying to create a facebook app just to learn and coming across a strange
I'm trying to create custom binding in knockout to use it as multiple selection
I have a view that will look like this: I'm trying to figure out
I'm new to ASP.NET MVC 3 and I'm trying to create a edit view
I am trying to implement a MVC application that has a view, controller and
I have multiple UI components that needs a navigation menu. I am trying to
I'm trying to figure out how to create additional calculated properties that are exposed
I'm trying to create a custom binding that will show a loading gif while

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.