1

我有以下课程:

public class DbCatalogEntry
{
    public int id { get; set; }
    private string filename;
    public long size { get; set; }
    public int duration { get; set; }
    public int height { get; set; }
    public int width { get; set; }
    public string vcodec { get; set; }
    public List<CatalogAudioStreamEntry> audiostreams { get; set; }
    public string imdbId { get; set; }
    public int ownedByUser { get; set; }
    public string mxHash { get; set; }
}


public class CatalogAudioStreamEntry
{
    public int bitrate { get; set; }
    public string codec { get; set; }
    public string language { get; set; }
    public int channels { get; set; }
    public string features { get; set; }
}

这是一对多的关系。我正在尝试DbCatalogEntry从数据库中检索对象列表并填写audiostreams相同的查询。

我尝试了以下方法,但它不起作用:

var MovieEntryList = 
    (from vwCat in ctx.sp_GetMovieCatalog(getCurrentUserId())
      select new DbCatalogEntry()
          {
              id = vwCat.id,
              size = vwCat.size,
              duration = vwCat.duration,
              vcodec = vwCat.codec,
              Filename = vwCat.filename,
              imdbId = vwCat.imdb_id,
              mxHash = vwCat.mxhash,
              ownedByUser = (int)vwCat.owned,
              width = vwCat.width,
              height =  vwCat.height,
              audiostreams = 
                (from astr in ctx.audiostreamentry
                  where astr.movie_id == vwCat.id
                  select new CatalogAudioStreamEntry()
                      {
                          bitrate = astr.bitrate,
                          channels = astr.channels,
                          codec = astr.codec,
                          features = astr.features,
                          language = astr.language
                      }).ToList()
          }).ToList();

搜索显示您无法将 ToList() 放入 linq to entity 查询,因为它无法转换。我阅读了有关更改为 IEnumerable 的多项建议audiostreams,但也无法使其正常工作。大多数尝试编译正常,但在运行时失败,无法创建类型的常量值...

谁能指出我解决这个问题的正确方向?重要的是必须将到数据库的往返次数保持在最低限度,这样就不可能为每个子查询创建一个客户端子查询DbCatalogEntry来填充列表。

更新#1:

提供更多详细信息:这是 EF 从我的数据库生成的模型: 模型

sp_GetMovieCatalog(getCurrentUserId()) 是 SQL 服务器上的一个存储函数,它接受一个用于过滤结果的参数。

我想要做的是从中选择movieenty并加载所有关联的行audiostreamenty。此数据应用于创建 的实例DbCatalogEntry,因此结果类型为List<DbCatalogEntry>

这有可能吗?也许有更好/更简单的解决方案?

4

1 回答 1

1

我相信,您有问题,因为尝试对多个查询使用相同的上下文

你可以这样尝试:

    var MovieEntryList = 
    (from vwCat in ctx.sp_MovieCatalog
    where vwCat.owned = currentUserId
    select new DbCatalogEntry()
      {
          id = vwCat.id,
          size = vwCat.size,
          duration = vwCat.duration,
          vcodec = vwCat.codec,
          Filename = vwCat.filename,
          imdbId = vwCat.imdb_id,
          mxHash = vwCat.mxhash,
          ownedByUser = (int)vwCat.owned,
          width = vwCat.width,
          height =  vwCat.height,
          audiostreams = vwCat.audiostreamentry.Select(astr=>
               new CatalogAudioStreamEntry()
                  {
                      bitrate = astr.bitrate,
                      channels = astr.channels,
                      codec = astr.codec,
                      features = astr.features,
                      language = astr.language
                  }).ToList()
      }).ToList();

或者这样:

    var MovieEntryList = ctx.audiostreamentry.where(p=>p.movieCatolog.ownedByUser - currentUserId)
    //.ToList() //if you call ToList() here futher will be LINQ to Object, and you want have most of problems
    .GroupBy(p=>new {Id = p.movieCatolog.id, Owner = p.movieCatolog.p.movieCatolog.id})
    .Select(p=> new DbCatalogEntry{
    id = p.Key.Id,
    ownedByUser = p.Key.Owner,
    audiostrams = p.Select(x=>new CatalogAudioStreamEntry
    {
                      bitrate = astr.bitrate,
                      channels = astr.channels,
                      codec = astr.codec,
                      features = astr.features,
                      language = astr.language
                  })

    })
于 2013-06-08T15:47:24.253 回答