使用Entity Framework 5,MS SQL Server CE。 LINQ查询(有效,选择带有选项的产品,选项值和具有相关选项值的产品数量):
var q = _db
.Products
.Where(p => p.ProductID == productName)
.Select(p => new {
p.ProductID,
p.Title,
Options = p
.Options
.Select(o => new {
o.OptionId,
o.Title,
Values = o
.Values
.Select(v => new {
v.OptionValueId,
v.Title,
v.OptionId,
CountProducts = _db.Products.Count(pp => pp.Options.Any(oo => oo.Values.Any(ooo => ooo.Title == v.Title)))
})
})
})
.FirstOrDefault();
生成SQL(无效,[令牌行号= 31,令牌行偏移= 139,错误令牌= SELECT]):
SELECT
[Project6].[ProductID] AS [ProductID],
[Project6].[Title] AS [Title],
[Project6].[C2] AS [C1],
[Project6].[OptionId] AS [OptionId],
[Project6].[Title1] AS [Title1],
[Project6].[C1] AS [C2],
[Project6].[OptionValueId] AS [OptionValueId],
[Project6].[Title2] AS [Title2],
[Project6].[OptionId1] AS [OptionId1],
[Project6].[C3] AS [C3]
FROM ( SELECT
[Project5].[ProductID] AS [ProductID],
[Project5].[Title] AS [Title],
[Project5].[OptionId] AS [OptionId],
[Project5].[Title1] AS [Title1],
[Project5].[OptionValueId] AS [OptionValueId],
[Project5].[Title2] AS [Title2],
[Project5].[OptionId1] AS [OptionId1],
CASE WHEN ([Project5].[OptionId] IS NULL) THEN CAST(NULL AS int) WHEN ([Project5].[OptionValueId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1],
CASE WHEN ([Project5].[OptionId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2],
[Project5].[C1] AS [C3]
FROM ( SELECT
[Project1].[ProductID] AS [ProductID],
[Project1].[Title] AS [Title],
[Project1].[OptionId] AS [OptionId],
[Project1].[Title1] AS [Title1],
[Project1].[OptionValueId] AS [OptionValueId],
[Project1].[Title2] AS [Title2],
[Project1].[OptionId1] AS [OptionId1],
CASE WHEN ([Project1].[OptionId] IS NULL) THEN CAST(NULL AS int) WHEN ([Project1].[OptionValueId] IS NULL) THEN CAST(NULL AS int) ELSE (SELECT
COUNT(1) AS [A1]
FROM [Products] AS [Extent4]
WHERE EXISTS (SELECT
1 AS [C1]
FROM ( SELECT
[Extent5].[OptionId] AS [OptionId]
FROM [Options] AS [Extent5]
WHERE [Extent4].[ProductID] = [Extent5].[Product_ProductID]
) AS [Project2]
WHERE EXISTS (SELECT
1 AS [C1]
FROM [OptionValues] AS [Extent6]
WHERE ([Project2].[OptionId] = [Extent6].[OptionId]) AND ([Extent6].[Title] = [Project1].[Title2])
)
)) END AS [C1]
FROM ( SELECT
[Extent1].[ProductID] AS [ProductID],
[Extent1].[Title] AS [Title],
[Join1].[OptionId1] AS [OptionId],
[Join1].[Title1] AS [Title1],
[Join1].[OptionValueId] AS [OptionValueId],
[Join1].[Title2] AS [Title2],
[Join1].[OptionId2] AS [OptionId1]
FROM [Products] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[OptionId] AS [OptionId1], [Extent2].[Title] AS [Title1], [Extent2].[Product_ProductID] AS [Product_ProductID], [Extent3].[OptionValueId] AS [OptionValueId], [Extent3].[Title] AS [Title2], [Extent3].[OptionId] AS [OptionId2]
FROM [Options] AS [Extent2]
LEFT OUTER JOIN [OptionValues] AS [Extent3] ON [Extent2].[OptionId] = [Extent3].[OptionId] ) AS [Join1] ON [Extent1].[ProductID] = [Join1].[Product_ProductID]
WHERE [Extent1].[ProductID] = @p__linq__0
) AS [Project1]
) AS [Project5]
) AS [Project6]
ORDER BY [Project6].[ProductID] ASC, [Project6].[C2] ASC, [Project6].[OptionId] ASC, [Project6].[C1] ASC
EF可能会生成错误的代码。