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

  • Home
  • SEARCH
  • 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 514695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:33:06+00:00 2026-05-13T07:33:06+00:00

I’m using LINQPad to learn LINQ and I’ve run into a stumbling block. The

  • 0

I’m using LINQPad to learn LINQ and I’ve run into a stumbling block.

The goal is to get a list of Network Ids, Network Names and how many Stations each has.

Here is my original SQL:

SELECT n.iStationId AS NetworkID, n.sPrettyName AS NetworkName, COUNT(s.iStationID) AS StationCount
FROM T_StationInfo AS s, T_StationInfo as n
WHERE s.iNetworkId = n.iStationId
GROUP BY n.sPrettyName, n.iStationId
ORDER BY COUNT(s.iStationID) DESC

Here is my LINQ:

from s in T_stationInfo
from n in T_stationInfo
where s.INetworkID == n.IStationID
group s by s.INetworkID into stations
orderby stations.Count(x => x.INetworkID == stations.Key) descending
select new {
    NetworkId = stations.Key,
    NetworkName = T_stationInfo.Single(x => x.IStationID == stations.Key).SPrettyName,
    StationCount = stations.Count(x => x.INetworkID == stations.Key)
};

LINQ takes 5 times longer to execute. I’m looking at the SQL that the linq statement generates and it pulls in the t_stationInfo table 7 times.

I believe this is because I am misusing LINQ but I don’t see where or how.

What LINQ statement would create equivalent SQL or, at least, SQL that isn’t so poor performing?

A couple notes:

  1. The structure of the table/database can not be changed.
  2. This question is more about learning to use LINQ than getting the list of ids, names, and counts.
  3. I do appreciate it! 🙂

–EDIT–

Just to clarify the structure:
Each row in the table is an entity that has various information (name, contact, etc) and can have a parent. Those parents are also in the table. In this case parents can’t have parents. Their parent field is NULL or 0.

So to get the Name of the Parent of a Station(called Network in the table), I pull the station info table in twice and join the parent id (network id) to the entity id (station id) so that on a single row I have the station’s info and the parent’s info. Hence the two froms of the same table.

Did that make sense?

–EDIT2–

This is the sql generated by the original LINQ query:

SELECT [t2].[iNetworkID] AS [NetworkId], (
    SELECT [t5].[sPrettyName]
    FROM [t_stationInfo] AS [t5]
    WHERE (CONVERT(Decimal(29,4),[t5].[iStationID])) = [t2].[iNetworkID]
    ) AS [NetworkName], (
    SELECT COUNT(*)
    FROM [t_stationInfo] AS [t6], [t_stationInfo] AS [t7]
    WHERE ([t6].[iNetworkID] = [t2].[iNetworkID]) AND ([t2].[iNetworkID] = [t6].[iNetworkID]) AND ([t6].[iNetworkID] = (CONVERT(Decimal(29,4),[t7].[iStationID])))
    ) AS [StationCount]
FROM (
    SELECT [t0].[iNetworkID]
    FROM [t_stationInfo] AS [t0], [t_stationInfo] AS [t1]
    WHERE [t0].[iNetworkID] = (CONVERT(Decimal(29,4),[t1].[iStationID]))
    GROUP BY [t0].[iNetworkID]
    ) AS [t2]
ORDER BY (
    SELECT COUNT(*)
    FROM [t_stationInfo] AS [t3], [t_stationInfo] AS [t4]
    WHERE ([t3].[iNetworkID] = [t2].[iNetworkID]) AND ([t2].[iNetworkID] = [t3].[iNetworkID]) AND ([t3].[iNetworkID] = (CONVERT(Decimal(29,4),[t4].[iStationID])))
    ) DESC
  • 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-13T07:33:06+00:00Added an answer on May 13, 2026 at 7:33 am

    I don’t how big of an impact this will have on your performance, if any. But when I look at your query I see one function declared twice:

    stations.Count(s => s.INetworkID == stations.Key)
    

    Does using a let clause improve performance at all?

    from station in T_stationInfo
    from network in T_stationInfo
    where station.INetworkID == network.IStationID
    group station by station.INetworkID into stations
    let stationCount = stations.Count(x => x.INetworkID == stations.Key)
    orderby stationCount descending
    select new
    {
        NetworkId = stations.Key,
        NetworkName = T_stationInfo.First(x => x.IStationID == stations.Key).sPrettyName,
        StationCount = stationCount
    };
    

    I feel like there should also be a better way to assign the NetworkName property, but I’m not sure.

    Oh, and sorry for renaming the variables. I changed s to station and n to network to help me follow it a little better.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.