我想在ASP.NET页面上使用EntityDataSource控件选择数据。给予数据源的参数由以下代码设置:
dsPlastics.Select = "top(10) it.PlasticId, it.Name, it.DateModified, it.Producer.Name as ProducerName, it.PlasticType.PlasticFamily.Name as PlasticFamilyName, it.PlasticType.Name as PlasticTypeName";
dsPlastics.Where = "it.Active == true";
dsPlastics.OrderBy = "it.DateModified DESC";
我期待与此类似的查询(根据需要选择数据 - 最后10条记录与相关数据相结合):
select top(10) PlasticId, pl.Name, DateModified, pr.Name as ProducerName, pf.Name as PlasticFamilyName, pt.Name as PlasticTypeName
from Plastics pl
left join Producers pr ON pl.ProducerId = pr.ProducerId
left join PlasticTypes pt ON pl.PlasticTypeId = pt.PlasticTypeId
left join PlasticFamilies pf ON pt.PlasticFamilyId = pf.PlasticFamilyId
where pl.Active = 1
order by pl.DateModified DESC
但是实体框架会生成这个(不会根据需要选择数据):
SELECT
[Limit1].[PlasticId] AS [PlasticId],
[Limit1].[Name] AS [Name],
[Limit1].[DateModified] AS [DateModified],
[Extent2].[Name] AS [Name1],
[Extent4].[Name] AS [Name2],
[Extent5].[Name] AS [Name3]
FROM (SELECT TOP (10) [Extent1].[PlasticId] AS [PlasticId], [Extent1].[ProducerId] AS [ProducerId], [Extent1].[PlasticTypeId] AS [PlasticTypeId], [Extent1].[Name] AS [Name], [Extent1].[DateModified] AS [DateModified]
FROM [dbo].[Plastics] AS [Extent1]
WHERE [Extent1].[Active] = 1 ) AS [Limit1]
LEFT OUTER JOIN [dbo].[Producers] AS [Extent2] ON [Limit1].[ProducerId] = [Extent2].[ProducerId]
LEFT OUTER JOIN [dbo].[PlasticTypes] AS [Extent3] ON [Limit1].[PlasticTypeId] = [Extent3].[PlasticTypeId]
LEFT OUTER JOIN [dbo].[PlasticFamilies] AS [Extent4] ON [Extent3].[PlasticFamilyId] = [Extent4].[PlasticFamilyId]
LEFT OUTER JOIN [dbo].[PlasticTypes] AS [Extent5] ON [Limit1].[PlasticTypeId] = [Extent5].[PlasticTypeId]
ORDER BY [Limit1].[DateModified] DESC
请问,我应该如何构建查询,以获取我需要的查询?
答案 0 :(得分:2)
你可以这样试试。
<asp:EntityDataSource ID="ProductDataSource" runat="server"
CommandText="select top(10) PlasticId, pl.Name, DateModified, pr.Name as ProducerName, pf.Name as PlasticFamilyName, pt.Name as PlasticTypeName
from Plastics pl
left join Producers pr ON pl.ProducerId = pr.ProducerId
left join PlasticTypes pt ON pl.PlasticTypeId = pt.PlasticTypeId
left join PlasticFamilies pf ON pt.PlasticFamilyId = pf.PlasticFamilyId
where pl.Active = 1
order by pl.DateModified DESC"
ConnectionString="name=AdventureWorksEntities"
DefaultContainerName="AdventureWorksEntities" >
</asp:EntityDataSource>
或尝试在Command Text属性中设置查询