我有两个应用程序:
API
Web窗体应用
我在API中使用Entity Framework 5.
Web窗体应用程序进行API调用并检索一些数据。
无论出于何种原因,当我收到数据时,它会给我几行完全相同的内容。
这里的问题是,当我使用SQL Server Profiler并查看查询时,查询是正确的,如果我接受此查询并在SQL中运行它,结果是正确的。但是,在Web窗体应用程序中,很多数据都是相同的。
看看这两个截图 -
运行应用程序:
获取实体框架生成的SQL查询并将其运行到SQL中:
正如你所看到的,这让我非常困惑......
有没有人知道这里的问题是什么?
这是我的EF模型:
实体框架生成的查询:
{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 '~'}
我通过进入调试模式得到了这个查询。如果我在实际数据库中运行它,它将返回正确的结果。
控制器代码:
public class ShowsController:ApiController { 私人只读TVDataEntities db;
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();
}
}
答案 0 :(得分:0)
问题已经解决。这是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
};