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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:37:57+00:00 2026-05-29T19:37:57+00:00

I have a linq to sql class with several table bound properties, but I

  • 0

I have a linq to sql class with several table bound properties, but I want to add another that is computed from a more complicated query.

Is there a way to add a property that contains custom SQL code while still maintaining efficiency?

public class Region
{
   string Name {get ;set;}
   DateTime Founding {get; set;}
   int CivilWarCasualties
   {
     get
     {
        // Scalar SQL code that returns an int
     }
   }
}

This is how I’m currently doing it (Please ignore the schema and I made it up on the fly). There are not enough one-to-one relationships to do this without doing joins and that obviously makes it more complicated. I use this big join in a bunch of places and I want to simplify it:

using (Context db = new Context())
{
  var q = (from r in db.GetTable<Region>()
          join b in db.GetTable<Battles>() join s.ID equals b.RegionID
          join s in db.GetTable<Soldier>() join s.LastBattleID equals b.ID
          join p in db.GetTable<Person>() join p.ID equals s.PersonID
          where !p.IsAlive
          select new {r, p})
       .GroupBy(x => x.Name)
       .Select(x=> new {x.Key.r.Name, x.Where(y => y.Key = x.Key).Count());
}

This is how I want to do it:

using (Context db = new Context())
{
  var q = (from r in db.GetTable<Region>()
           select new {r.Name, r.CivilWarCasualties})
}

I basically want to manually write the SQL code for that big complicated join above once (that I probably didn’t get right because I’m making it up as I go as an example).

I’m trying to avoid the “no translation to SQL” error that happens if I try and do a property in C#. I’m also open to other ideas.

  • 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-29T19:37:59+00:00Added an answer on May 29, 2026 at 7:37 pm

    I would probably create a SQL view that contained the computed column. SQL Server can then do a good job of optimizing the query.

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

Sidebar

Related Questions

I have a LINQ to SQL class VoucherRecord based on a simple table. One
I have a class containing Linq To SQL objects that are used to populate
In ASP.NET MVC 2, I have a Linq to sql class that contains a
I have a LINQ to SQL class that I am reading into a List.
Suppose I have a linq to sql class. I want to return the object
I have a Linq-to-SQL class, and I'd like to perform some pre-save validation before
I have a LINQ to SQL generated class with a readonly property: <Column(Name:=totalLogins, Storage:=_TotalLogins,
What I have: LINQ to SQL auto generated class, which corresponds to the shape
I have a Linq-To-Sql based repository class which I have been successfully using. I
I have a class which provides generic access to LINQ to SQL entities, for

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.