I am attempting to figure out how to count the matching rows on the table while using EntityFramework.

However , each row may have many mb of information (inside a Binary area). Obviously the SQL could be something similar to this:

SELECT COUNT(*) FROM [MyTable] WHERE [fkID] = '1';

I possibly could load all the rows and then discover the Count with:

var owner = context.MyContainer.Where(t => t.ID == '1');
owner.MyTable.Load();
var count = owner.MyTable.Count();

But that's grossly inefficient. It is possible to simpler way?


EDIT: Thanks, all. I have moved the DB from the private attached in order to run profiling this can help but causes confusions I did not expect.

And my real information is a little much deeper, I'll use Trucks transporting Pallets of Cases of Products -- and I'm not going the Truck to depart unless of course there's a minumum of one Item inside it.

My attempts are proven below. The part I do not get is the fact that Situation_2 never access the DB server (MSSQL).

var truck = context.Truck.FirstOrDefault(t => (t.ID == truckID));
if (truck == null)
    return "Invalid Truck ID: " + truckID;
var dlist = from t in ve.Truck
    where t.ID == truckID
    select t.Driver;
if (dlist.Count() == 0)
    return "No Driver for this Truck";

var plist = from t in ve.Truck where t.ID == truckID
    from r in t.Pallet select r;
if (plist.Count() == 0)
    return "No Pallets are in this Truck";
#if CASE_1
/// This works fine (using 'plist'):
var list1 = from r in plist
    from c in r.Case
    from i in c.Item
    select i;
if (list1.Count() == 0)
    return "No Items are in the Truck";
#endif

#if CASE_2
/// This never executes any SQL on the server.
var list2 = from r in truck.Pallet
        from c in r.Case
        from i in c.Item
        select i;
bool ok = (list.Count() > 0);
if (!ok)
    return "No Items are in the Truck";
#endif

#if CASE_3
/// Forced loading also works, as stated in the OP...
bool ok = false;
foreach (var pallet in truck.Pallet) {
    pallet.Case.Load();
    foreach (var kase in pallet.Case) {
        kase.Item.Load();
        var item = kase.Item.FirstOrDefault();
        if (item != null) {
            ok = true;
            break;
        }
    }
    if (ok) break;
}
if (!ok)
    return "No Items are in the Truck";
#endif

And also the SQL caused by Situation_1 is piped through sp_executesql, but:

SELECT [Project1].[C1] AS [C1]
FROM   ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]
LEFT OUTER JOIN  (SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(cast(1 as bit)) AS [A1]
        FROM   [dbo].[PalletTruckMap] AS [Extent1]
        INNER JOIN [dbo].[PalletCaseMap] AS [Extent2] ON [Extent1].[PalletID] = [Extent2].[PalletID]
        INNER JOIN [dbo].[Item] AS [Extent3] ON [Extent2].[CaseID] = [Extent3].[CaseID]
        WHERE [Extent1].[TruckID] = '....'
    )  AS [GroupBy1] ) AS [Project1] ON 1 = 1

[I do not genuinely have Trucks, Motorists, Pallets, Cases or Products as you can tell in the SQL the18 wheeler-Pallet and Pallet-Situation associations are lots of-to-many -- although I do not believe that matters. My real objects are intangibles and harder to explain, and so i transformed what they are called.]

var count = (from o in context.MyContainer
             where o.ID == '1'
             from t in o.MyTable
             select t).Count();

I believe you would like something similar to

var count = context.MyTable.Count(t => t.MyContainer.ID == '1');

(edited to mirror comments)