首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用EntityDataSource连接

使用EntityDataSource连接
EN

Stack Overflow用户
提问于 2013-11-05 13:09:55
回答 1查看 2.3K关注 0票数 2

我希望使用EntityDataSource控件在ASP.NET页面上选择数据。给数据源的参数由以下代码设置:

代码语言:javascript
复制
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";

Im期待类似于此的查询(根据需要选择数据-与相关数据连接的最后10条记录):

代码语言:javascript
复制
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

但是实体框架会产生这种情况(不按我的需要选择数据):

代码语言:javascript
复制
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

请,我应该如何构建查询,以获得我需要的查询?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-05 14:58:22

你可以这样做。

代码语言:javascript
复制
<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>

或尝试在“命令文本”属性中设置查询

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19789776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档