1

我有两个应用程序:

  1. API

  2. 网络表单应用程序

我在 API 中使用实体框架 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 '~'}

我通过进入调试模式得到了这个查询。如果我在实际数据库中运行它,它会返回正确的结果。

控制器代码:

公共类 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();
}

}

4

1 回答 1

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
                                             };
于 2013-06-22T00:35:57.817 回答