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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:56:15+00:00 2026-06-17T11:56:15+00:00

I have 2 tables that I need to get data from…Rooms and Address. The

  • 0

I have 2 tables that I need to get data from…Rooms and Address. The Rooms table has an AddressID field in it but it can be null, so the relationship is 0 to 1. I’ve been trying to write a LINQ statement that will return a specific Room AND the Address information if it exists. My model is defined as so:

public partial class Room
{
    public Room()
    {
        this.Address = new HashSet<Address>();
    }

    public int RoomID { get; set; }
    public Nullable<int> AddressID { get; set; }
    public string Comments { get; set; }
    public string Notes { get; set; }

    public virtual ICollection<Address> Address { get; set; }
}

I tried to use the .Include(“Address”) on the linq statement but it didn’t work and I believe it’s caused by the Join statement (which I read, once it’s used, it silently drops the Include).

Here’s an ugly way around it but I know there’s a better way:

var csdDB = new CSDContext(CustomerCode);

IList<Room> rooms = (from r in csdDB.Rooms
                        join sr in csdDB.SiteRooms
                            on r.RoomID equals sr.RoomID
                        where sr.SiteID == id
                        orderby r.RoomName
                        select r).ToList<Room>();

int? addressID = rooms.FirstOrDefault<Room>().AddressID;

if (addressID != null)
{
    IList<Address> address = (from a in csdDB.Addresses
                                where a.AddressID == addressID
                                select a).ToList<Address>();

    rooms.FirstOrDefault<Room>().Address = address;
}

I tried to do the filtering directly on the results:

IList<Address> address = (from a in csdDB.Addresses
                            where a.AddressID == rooms.FirstOrDefault().AddressID
                            select a).ToList<Address>();

But it throws an error:

Unable to create a constant value of type 'Models.CSD.Room'. Only primitive types or enumeration types are supported in this context.

Any suggestions on how to make this cleaner/better is appreciated!

  • 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-17T11:56:17+00:00Added an answer on June 17, 2026 at 11:56 am

    Incorporating a left-join in LINQ might look something like this:

    var results = (from r in csdDB.Rooms
                   join sr in csdDB.SiteRooms
                       on r.RoomID equals sr.RoomID && sr.SiteID equals id
                   join addr in csdDB.Addresses
                       on r.AddressID equals addr.AddressID into roomAddrs
                   from roomAddr in roomAddrs.DefaultIfEmpty(new Address())
                   select new 
                   {
                       Room = r,
                       Address = roomAddr
                   }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got 2 tables that I need to get data from...my users table looks
I have two tables that I need to combine to get the data I
I have 2 tables that I need to get information from, and would like
OK, I need to get some data from multiple tables, all of which have
I have two tables that have data that I need a count from Login
I have a database two tables and a linking table that I need a
I have a table of data that I need to dynamically add a column
I have 2 tables that I import to EF model. First table has a
I have to make a view that shows unrelated data from multiple tables. I
I have few tables that gather data for internal use but i want to

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.