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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:03:51+00:00 2026-05-18T01:03:51+00:00

The following TSQL provides an example of how I might solve this problem with

  • 0

The following TSQL provides an example of how I might solve this problem with SQL. The goal is to return 1 row per OID from the Left Table, where the Count of the records in the left table is equal to the Count of the matching rows in the right table.

SELECT cs.OID, Count(cs.OID) AS CarCount, Sum(RS.Check) AS RoadCount  
   FROM Cars AS cs  
LEFT JOIN Roads AS RS  
  ON CS.oid = RS.OID  
   AND cs.RID = RS.RID  
  GROUP BY cs.OID  
  HAVING Count(cs.OID) = Sum(RS.Check)  

Using object setup below, is there an equivalent LINQ query that can be constructed, or is this not possible? Note the default value given for check in the declaration of the Road class. In the setup example below the result should be zero matching. Adding one more road with proper values will have it return just one. At least that is what is ideal.

The problem I have run into is that it seems this type of TSQL code is just too complex for LINQ. I haven’t found an obvious or non-obvious solution to achieve a similar behavior. Thus I believe perhaps the solution is to stop trying to copy SQL and do something different. With not enough LINQ experience, I wouldn’t know where to start.

public class Roads : List<Road>{}  
public class Road  
{  
    public int RID;  
    public int OID;  
    public int check = 1;  
}  
public class Cars : List<Car> { }  
public class Car  
{  
    public int RID;  
    public int OID;  
}  

private void CheckCheck()  
{  
    Roads rs = new Roads();  
    Cars cs = new Cars();  

    Car c = new Car();  
    c.OID = 1;  
    c.RID = 1;  
    cs.Add(c);  
    c = new Car();  
    c.OID = 1;  
    c.RID = 2;  
    cs.Add(c);  
    c = new Car();  
    c.OID = 1;  
    c.RID = 3;  
    cs.Add(c);  

    Road r = new Road();  
    r.OID = 1;  
    r.RID = 1;  
    rs.Add(r);  
    r = new Road();  
    r.OID = 1;  
    r.RID = 2;  
    rs.Add(r);  

    // Results should be :  
    // OID where Count of OID from C = Count of OID from R  
}  
  • 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-18T01:03:52+00:00Added an answer on May 18, 2026 at 1:03 am
    • Your HAVING clause would filter out any cars which do not match roads. This makes the left join into an inner join.
    • You have COUNT(cs.OID) which says it’s counting cars, but it doesn’t. You might have meant COUNT(DISTINCT cs.OID)

    Here’s a literal translation:

    from c in Cars
    join r in Roads on new {c.OID, c.RID} equals new {r.OID, r.RID}
    group new {Car = c, Road = r} by c.OID into g
    let carCount = g.Count()  //did you mean g.Select(x => x.Car.OID).Distinct().Count()
    let roadCount = g.Sum(x => x.Road.Check)
    where carCount = roadCount
    select new {OID = g.Key, CarCount = carCount, RoadCount = roadCount}
    

    The goal is to return 1 row per OID from the Left Table, where the Count of the records in the left table is equal to the Count of the matching rows in the right table.

    Based on this description, I’d write:

    var carLookup = Cars.ToLookup(c => c.OID);
    var roadLookup = Roads.ToLookup(r => r.OID);
    
    from x in carLookup
    let carCount = x.Count()
    let roadCount = roadLookup[x.Key].Count()
    where carCount = roadCount
    select new {OID = g.Key, CarCount = carCount, RoadCount = roadCount}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following tsql code in sql server 2008: declare @ID INT SET @ID
I have the following TSQL Statement: SELECT ProductId, OwnerId FROM Product How I can
In my TSQL table I added a constraint with the following sQL statement alter
I have the following T-SQL query to delete a record from a series of
Consider the following TSQL: declare @xml xml select @xml = '<test xmlns=http://this-is-the-default-namespace-uri>some data</test>' select
Consider the following tsql... create function dbo.wtfunc(@s varchar(50)) returns varchar(10) begin return left(@s, 2);
I'm having trouble translating the following tSQL to LINQ to SQL in C#. Any
I've come across the following t-sql: SELECT {d'9999-12-31'} Which returns 9999-12-31 00:00:00.000 . This
Can anyone tell me if writing a query in the following tsql syntax is
I have the following short version of a tsql if else if.. IF @var

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.