Hier sind meine Tabellen:Warum fügt Linq-to-SQL unnötiges COUNT() hinzu?
create table customer
(
cid int primary key,
name varchar(32)
)
create table ord
(
oid int primary key,
cid int foreign key references customer,
address varchar(20)
)
Hier ist meine Linq-to-SQL-Anweisung:
var aaa = from c in db.Customer
select new { c, o = c.Ord.ToList() };
Und hier ist die unerklärliche Abfrage generiert durch Linq-to-SQL:
SELECT
[t0].[cid] AS [Cid], [t0].[name] AS [Name], [t1].[oid] AS [Oid],
[t1].[cid] AS [Cid2], [t1].[address] AS [Address],
(SELECT COUNT(*)
FROM [dbo].[ord] AS [t2]
WHERE [t2].[cid] = [t0].[cid]) AS [value]
FROM [dbo].[customer] AS [t0]
LEFT OUTER JOIN [dbo].[ord] AS [t1] ON [t1].[cid] = [t0].[cid]
ORDER BY [t0].[cid], [t1].[oid]
Ich möchte verstehen, wie man diesen COUNT(*)
Teil loswird. Es ist völlig unangebracht!
Möglicherweise verwandt? https://stackoverflow.com/questions/11997048/why-does-linq-2-sql-create-extra-uncesses-objects/12045296#12045296 – DenNukem