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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:31:41+00:00 2026-05-26T15:31:41+00:00

Here is simplified SQL of my tables, which is converted to LINQ to SQL

  • 0

Here is simplified SQL of my tables, which is converted to LINQ to SQL model.

CREATE TABLE Campaign (
    Id int PRIMARY KEY,
    Name varchar NOT NULL
);
CREATE TABLE Contract (
    Id int PRIMARY KEY,
    CampaignId int NULL REFERENCES Campaign(Id)
);

Now i have classes like this (these are in different namespace, not entity classes from datamodel).

public class CampaignInfo {
    public static CampaignModel Get(DataModel.CampaignInfo campaign) {
         return new CampaignInfo {
              Id = campaign.Id,
              Name = campaign.Name,
              Status = CampaignStatus.Get( c )
         };
    }
    public int Id {get; set;}
    public int Name {get; set;}
    public CampaignStatus { get; set;}
}

public class CampaignStatus {
    public static CampaignStatus Get(DataModel.Campaign campaign) {
         return new CampaignStatus {
              Campaign = campaign.Id, // this is just for lookup on client side
              ContractCount = campaign.Contracts.Count()
              // There is much more fields concerning status of campaign
         };
    }
    public int Campaign { get; set; }
    public int ContractCount {get; set;}
}

And than i am running query:

dataContext.Campaigns.Select( c => CampaignInfo.Get( c ) );

Other piece of code can do something like this:

dataContext.Campaigns.Where( c => c.Name == "DoIt" ).Select( c => CampaignInfo.Get( c ) );

Or i want to get list of statuses for campaigns:

dataContext.Campaigns.Select( c => CampaignStatus.Get( c ) );

Note: Results from those calls are converted to JSON, so there is no need to keep track on original db entities.

As you can see, there are two goals. To have control over what data are taken from database and reuse those structures in other places. However this approach is a huge mistake, because of that classes returning object and using it in expression tree.

Now i understand, it cannot magically create expression to make whole thing with one query. Instead it’s getting count for every campaign separately. In rather complex scenarios it’s ugly slowdown.

Is there some simple way how to achieve this ? I guess, those classes should return some expressions, but i am totally new to this field and i am not sure what to do.

  • 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-26T15:31:41+00:00Added an answer on May 26, 2026 at 3:31 pm

    The general problem, if I understand correctly, is that you have some business logic that you don’t want to have repeated throughout your code (DRY). You want to be able to use this logic inside your LINQ methods.

    The general solution with LINQ (over Expression trees) is to create a filter or transformation function that returns an IQueryable so you can do further processing on that query, before it is sent to the database.

    Here is a way to do this:

    // Reusable method that returns a query of CampaignStatus objects
    public static IQueryable<CampaignStatus> 
        GetCampaignStatusses(this IQueryable<Compaign> campaigns)
    {
        return
            from campaign in campaigns
            new CampaignStatus
            {
                Campaign = campaign.Id,
                ContractCount = compaign.Contracts.Count()
            };
    }
    

    With this in place, you can write the following code:

    using (var db = new DataModel.ModelDataContext() ) 
    {
        return
            from campaign in db.Campaigns.WithContractCount()
            from status in db.Campaigns.GetCampaignStatusses()
            where campaign.Id == status.Campaign
            select new
            {
                Id = campaign.Id,
                Name = campaign.Name,
                Status = status
            };
    }
    

    Having an method that returns an IQueryable allows you to do all sorts of extra operations, without that method knowing about it. For instance, you can add filtering:

            from campaign in db.Campaigns.WithContractCount()
            from status in db.Campaigns.GetCampaignStatusses()
            where campaign.Id == status.Campaign
            where campaign .Name == "DoIt"
            where status .ContractsCount < 10
            select new
            {
                Id = campaign.Id,
                Name = campaign.Name,
                Status = status
            };
    

    or add extra properties to the output:

            from campaign in db.Campaigns.WithContractCount()
            from status in db.Campaigns.GetCampaignStatusses()
            where campaign.Id == status.Campaign
            select new
            {
                OtherProp = campaign.OtherProp,
                Id = status.Campaign,
    
                Name = campaign.Name,
                Status = status
            };
    

    This will be translated to an efficient SQL query. The query will not get more records or columns from the database than strictly needed.

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

Sidebar

Related Questions

Here's my simplified table (SQL Server 2005): table1: col1 int, col2 int, col3 cast(col1/col2
Here is a simplified version of my database model. I have two tables: Image,
I have a linq to sql database. Very simplified we have 3 tables, Projects
Just getting into SQL stored queries right now... anyway, here's my database schema (simplified
I've got 3 tables that are something like this (simplified here ofc): users user_id
I have a table schema similar to the following (simplified): CREATE TABLE Transactions (
Given the following tables: Resources: ID int, Name varchar(100), Address varchar(500), City varchar(100), etc.
I have this table for documents (simplified version here): id rev content 1 1
Using SQL Server 2008. Here is a simplified example of my problem: WITH cte
Here is a simplified version of my table: group price spec a 1 .

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.