Wikipedia:Reference desk/Archives/Computing/2020 November 14

= November 14 =

Count only rows linking to another table in Linq to SQL
I ran to a problem today at work. There is a Linq to SQL query in a C# class similar to this:

The intent of this query is to select all rows in TableA with their ID numbers and the number of rows in TableB linking to those rows. This query works all OK, but I found out that what is actually needed is not the count of all rows in TableB linking to the specific rows in TableA, but instead the count of all rows in TableB linking to both the specific rows in TableA and to at least some row in a third table, let's call it TableC. The contents of the rows in TableC don't matter, it's enough that they exist. In other words, I want to add a  condition. How can I do that in Linq to SQL? J I P &#124; Talk 00:16, 14 November 2020 (UTC)


 * You might try adding one of the following conditions to the subselect: I haven't tried them, so I don't know for sure which will translate successfully to SQL or how efficient each might be. --  Tom N  talk/contrib 01:49, 14 November 2020 (UTC)
 * I used the first one and it did the job. I haven't tested which of them is the most efficient. J I P  &#124; Talk 15:31, 17 November 2020 (UTC)