C# 返回许多完全相同的行的API
我有两份申请: 原料药 Web表单应用程序 我在API中使用实体框架5 Web表单应用程序进行API调用并检索一些数据 不管出于什么原因,当我得到数据时,它会给我几行完全相同的数据 这里的问题是,当我使用SQL Server Profiler并查看查询时,查询是正确的,如果我接受此查询并在SQL中运行它,则结果是正确的。然而,在Web表单应用程序中,许多数据都返回相同的结果 看这两张截图- 运行应用程序: 获取实体框架生成的SQL查询并将其运行到SQL中: 正如你所看到的,这让我很困惑 有人知道这是什么问题吗 以下是我的EF模型: 实体框架生成的查询:C# 返回许多完全相同的行的API,c#,asp.net,entity-framework-5,asp.net-web-api,C#,Asp.net,Entity Framework 5,Asp.net Web Api,我有两份申请: 原料药 Web表单应用程序 我在API中使用实体框架5 Web表单应用程序进行API调用并检索一些数据 不管出于什么原因,当我得到数据时,它会给我几行完全相同的数据 这里的问题是,当我使用SQL Server Profiler并查看查询时,查询是正确的,如果我接受此查询并在SQL中运行它,则结果是正确的。然而,在Web表单应用程序中,许多数据都返回相同的结果 看这两张截图- 运行应用程序: 获取实体框架生成的SQL查询并将其运行到SQL中: 正如你所看到的,这让我很困惑 有人知道
{SELECT
[Extent1].[dataSource] AS [dataSource],
[Extent1].[ShowId] AS [ShowId],
[Extent1].[Title] AS [Title],
[Extent1].[EpisodeId] AS [EpisodeId],
[Extent1].[EpisodeTitle] AS [EpisodeTitle],
[Extent1].[Genre] AS [Genre],
[Extent1].[ShowTypeDescription] AS [ShowTypeDescription],
[Extent1].[DirectorName] AS [DirectorName],
[Extent1].[ReleaseYear] AS [ReleaseYear],
[Extent1].[SeasonEpisode] AS [SeasonEpisode]
FROM (SELECT
[TVData_VW_ShowList].[dataSource] AS [dataSource],
[TVData_VW_ShowList].[ShowId] AS [ShowId],
[TVData_VW_ShowList].[Title] AS [Title],
[TVData_VW_ShowList].[EpisodeId] AS [EpisodeId],
[TVData_VW_ShowList].[EpisodeTitle] AS [EpisodeTitle],
[TVData_VW_ShowList].[Genre] AS [Genre],
[TVData_VW_ShowList].[ShowTypeDescription] AS [ShowTypeDescription],
[TVData_VW_ShowList].[DirectorName] AS [DirectorName],
[TVData_VW_ShowList].[ReleaseYear] AS [ReleaseYear],
[TVData_VW_ShowList].[SeasonEpisode] AS [SeasonEpisode]
FROM [dbo].[TVData_VW_ShowList] AS [TVData_VW_ShowList]) AS [Extent1]
WHERE [Extent1].[Title] LIKE @p__linq__0 ESCAPE '~'}
我是通过进入调试模式得到这个查询的。如果我在实际数据库中运行它,它将返回正确的结果
控制器代码:
公共类ShowsController:ApiController
{
私有只读TVDataEntities数据库
public ShowsController()
{
db = new TVDataEntities();
db.Configuration.ProxyCreationEnabled = false;
}
public IEnumerable<TVData_VW_ShowList> GetTVData_VW_ShowList(string dataSource = null, string title = null,
string episodeTitle = null, string genre = null,
string showTypeDescription = null,
string directorName = null,
string releaseYear = null,
string seasonEpisode = null)
{
var query = from s in db.TVData_VW_ShowList select s;
if (dataSource != null)
{
if (dataSource != "all")
{
query = query.Where(s => s.dataSource.Contains(dataSource));
}
}
if (title != null)
{
query = query.Where(s => s.Title.Contains(title));
}
if (episodeTitle != null)
{
query = query.Where(s => s.EpisodeTitle.Contains(episodeTitle));
}
if (genre != null)
{
query = query.Where(s => s.Genre.Contains(genre));
}
if (showTypeDescription != null)
{
query = query.Where(s => s.ShowTypeDescription.Contains(showTypeDescription));
}
if (directorName != null)
{
query = query.Where(s => s.DirectorName.Contains(directorName));
}
if (releaseYear != null)
{
query = query.Where(s => s.ReleaseYear.ToString().Contains(releaseYear));
}
if (seasonEpisode != null)
{
query = query.Where(s => s.SeasonEpisode.Contains(seasonEpisode));
}
return query.ToList();
}
}问题已经解决。这是Linq查询 这是正确的答案:
IQueryable<ETSShows> query = from shows in db.ETS_Shows
from episodes in
db.ETS_Episodes.Where(v => v.ShowId == shows.ShowId).DefaultIfEmpty()
from genres in
db.ETS_LKP_Genres.Where(s => s.GenreCode == shows.GenreCode).DefaultIfEmpty()
from showTypes in
db.ETS_LKP_ShowTypes.Where(z => z.ShowTypeCode == shows.ShowTypeCode).DefaultIfEmpty()
from directors in
db.ETS_Directors.Where(p => p.ShowId == shows.ShowId).DefaultIfEmpty()
select new ETSShows
{
DataSource = "ETS",
Title = shows.Title,
EpisodeTitle = episodes.EpisodeTitle,
Genre = genres.GenreDescription,
ShowTypeDescription = showTypes.ShowTypeDescription,
DirectorName = directors.Name,
ReleaseYear = (int)shows.ReleaseYear,
SeasonEpisode = episodes.SeasonEpisode,
ShowId = shows.ShowId,
EpisodeId = episodes.EpisodeId
};
我会一步一步地调试这个。首先从源头开始;忽略您的Web表单应用程序并自行调用API。你有重复的数据吗?您从实体框架收集的结果是否包含正确数量的结果?如果我通过浏览器调用API,它也会带回错误的数据。这对我来说没有任何意义,因为生成的查询是正确的,并且在SQL中运行时返回正确的行数…您可以在代码中放置断点或写入磁盘或任何其他调试方法来验证实体框架的结果吗?基本上,只需验证EF返回的内容与您在链中稍后对结果集所做的操作相比较。此外,如果您确实认为EF返回的是重复记录,则应在获取数据的位置发布代码。我在OP中添加了从EF获得的查询