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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:14:33+00:00 2026-05-30T02:14:33+00:00

I want to run a linq query that will return values to my custom

  • 0

I want to run a linq query that will return values to my custom DTO. This particular linq query will need to take into account joins from multiple tables, using switch case statements, count (*) and group by

This is the SQL version sample of the query I will need a LinQ equivalent of…

select 
slm.SLType, 
count(c.EStatID) as EStat, 
COUNT(cpd.TrId) as Training,
COUNT(
CASE WHEN cpd.TrStat=  44 THEN  1 
     WHEN cpd.TrStat!=  44 THEN NULL 
     WHEN cpd.TrStat IS NULL  THEN  NULL 
END) as TrainingComplete,
COUNT(
CASE WHEN cpd.CndAssess =  44 THEN  1 
     WHEN cpd.CndAssess !=  44 THEN NULL 
     WHEN cpd.CndAssess IS NULL  THEN  NULL 
END) as AssessmentComplete
from TabC c , TabCPD cpd, TabSLM slm 
where cpd.SLid = slm.SLid
and c.Id= cpd.CID
and c.O_Id = 1
group by slm.SLType 

It returns records in the following format. I have put each record as a new line with fields separated by commas. The numbers below are just as an example

TypeA, 0 , 1 , 1, 0 
TypeB, 1 , 0 , 1, 0

I am trying to create a linq query in the format like the one below without much luck

var query = 
    from c in TabC, ...... 
    select new MyCustomTableC_DTO
    {
      DTOproperty = c.MatchingTable property,....
    }

MyCustomTableC_DTO will have a property for each field in the query. Any idea how to accomplish this? The query I will use to build a list of type MyCustomTableC_DTO

Thanks for your time…

  • 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-30T02:14:34+00:00Added an answer on May 30, 2026 at 2:14 am

    When you try to convert that SQL statement to LINQ line by line, you would get something like this:

    from row in (
        from c in db.TabC
        from cpd in db.TabPD
        from slm in db.TabSLM
        where cpd.SLid == slm.SLid
        where c.Id == cpd.CID
        where c.O_Id == 1
        select new { c, cpd, slm })
    group row in row.slm.SLType into g
    select new
    {
        SLType = g.Key,
        EStat = g.Count(r => r.c.EstatID != null),
        Training = g.Count(r => r.cpd.TrId != null),
        TrainingComplete =
            g.Count(r => r.cpd.TrStat == 44),
        AssessmentComplete =
            g.Count(r => r.cpd.CndAssess == 44)
    };
    

    This query however, over complicates things and completely ignores that fact that Entity Framework knows much more about the model and generates all foreign keys as properties on the entities. Besides that, with LINQ, you must often approach things the other way around. In your case for instance, don’t start with the TabC or TabSLM entity, but TabPD, since that table is the cross-table. With this knowledge, we can write the LINQ query like this:

    from cpd in db.TabCPDs
    where cpd.TabC.O_Id == 1
    group cpd by cpd.TabSLM.SLType into g
    select new
    {
        SLType = g.Key,
        EStat = g.Count(r => r.TabC.EstatID != null),
        Training = g.Count(r => r.TrId != null),
        TrainingComplete =
            g.Count(r => r.TrStat == 44),
        AssessmentComplete =
            g.Count(r => r.CndAssess == 44)        
    };
    

    This is much simpler and (if I’m not mistaking) has the same result.

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

Sidebar

Related Questions

I have heard that Linq query write once can be run on SQL and
I have a LINQ query I wish to run and drop the result into
Why this code don't work,when i want run this code vwd 2008 express show
I want to run javascript/Python/Ruby inside my application. I have an application that creates
I want to run a background task that reads input from a TextReader and
This is my first crack at a method that is run periodically during the
This is a resource-allocation problem. My goal is to run a query to fetch
When I type a LINQ query, intellisense in Visual Studio 2010 shows that the
Some background: I have an database that I want to use linq-to-sql to update
I want run a script as follows: runner: ssh 'java program &' ssh 'java

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.