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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:31:32+00:00 2026-05-25T21:31:32+00:00

I would like to achieve the following sql query in Linq to SQL. SELECT

  • 0

I would like to achieve the following sql query in Linq to SQL.

 SELECT     TOP dbo.SiteDept.SiteDeptId, dbo.Site.SiteName + ' / ' + dbo.Dept.DeptName AS SiteDeptName, dbo.SiteDept.SiteId, dbo.SiteDept.DeptId, 
                      dbo.Dept.DeptName
FROM         dbo.SiteDept INNER JOIN
                      dbo.Site ON dbo.SiteDept.SiteId = dbo.Site.SiteId INNER JOIN
                      dbo.Dept ON dbo.SiteDept.DeptId = dbo.Dept.DeptId

I have a Linq to SQL data context with both a site and Dept entity and a SiteDept Entity that associates departments to the sites.

I have also added in a custom field in the SiteDept partial class for the SiteDeptName.

I was thinking something along the following lines.

   public IEnumerable<SiteDept> GetAllSiteDepts()
    {
        var dataContext = new AtomWebDataContext(_connectionString);

        var allSiteDepts = from sd in dataContext.SiteDepts
                           join s in dataContext.Sites
                               on sd.SiteId equals s.SiteId
                           join d in dataContext.Depts
                               on sd.DeptId equals d.DeptId
                           select new SiteDept()
                                      {
                                          SiteDeptId = sd.SiteDeptId,
                                          SiteId = sd.SiteId,
                                          DeptId = sd.DeptId,
                                          SiteDeptName = s.SiteName + "/" + d.DeptName
                                      };
        return allSiteDepts;
    }

However I get a “Explicit construction of entity type ‘GPSO.Repository.SiteDept’ in query is not allowed.”

Whats the best way to achieve what I want?

  • 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-05-25T21:31:33+00:00Added an answer on May 25, 2026 at 9:31 pm

    The problem is that SiteDept is one of your entities and it won’t let you create one directly. The way to handle this is by setting up the relationships between the entities and having LINQ to SQL fetch them all, then use the property on the partial class to fill in the name you need.

    public IEnumerable<SiteDept> GetAllSiteDepts()
    {
        var dataContext = new AtomWebDataContext(_connectionString);
    
        var allSiteDepts = from sd in dataContext.SiteDepts
                           select s;
    
        return allSiteDepts.ToList();
    }
    
    // Site and Dept are EntityRefs on SiteDept and Site, respectively
    public partial class SiteDept
    {
         public string SiteDeptName
         {
             get { return this.SiteName + "/" + this.Site.Dept.Name; }
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to form an SQL query to achieve the following: I have
I would like to achieve the following: I want a free Application Lifecycle Management
I would like to achieve the following: On homepage load, display modal box Within
I would like to achieve something very similar to Microsoft Access query designer -
Let's say I have the following SQL query: SELECT Meeting.id AS meetingId, Bill.id AS
I would like to achieve the following behaviour, but I'm not sure how: User
I'm trying to achieve the following in SQL Server 2005: SELECT (IF EITHER EXISTS)
I would like to achieve something similar to how scala defines Map as both
Thanks for a solution in C , now I would like to achieve this
I would like to be able to achieve something like this: class Zot {

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.