如何使用 Ria 服务自动加载与实体相关的详细信息(带条件)?

发布于 2024-12-08 15:14:29 字数 962 浏览 0 评论 0原文

我正在使用 Silverlight 4 和 Entity Framework 4 开发一个项目,并且尝试在客户端加载 EntityQuery 时自动加载与实体关联的详细信息(带条件)。

到目前为止,我已经能够使用 Include 属性制定一个解决方案,该解决方案返回与主实体关联的所有详细信息。我在这里缺少的是能够根据某些标准过滤掉详细信息。

作为示例,我的实体如下所示:

实体 Movie

Id (int)

[Include]
MovieLocalizedInformations (EntityCollection<MovieLocalizedInformation>)

实体 MovieLocalizedInformation

Id (int)
Movie_Id (int)
LanguageCode (eg.: en)
Title

在我的 DomainService 对象上,我公开了以下方法:

public IQueryable<Movie> GetMovies( string languageCode )
{
  return this.ObjectContext.Movies.Include( "MovieLocalizedInformations" );
}

这工作正常。但是,当我尝试添加 where 子句来根据语言代码过滤掉本地化信息时,只有电影会加载到客户端上。

有没有一种方法可以在一个查询中实现过滤?

注意:我还在客户端上使用 DomainDataSource分页,因此解决方案需要与之配合。

任何帮助将不胜感激!

谢谢,

雅克。

I'm developing a project using Silverlight 4 and Entity Framework 4 and I'm trying to auto-load the details (with conditions) associated with an entity when the client loads the EntityQuery.

So far, I've been able to put in place a solution, using the Include attribute, that returns all the details associated with the master entity. What I'm missing here is to be able to filter out the details based on some criteria.

As an example, here's what my entities look like:

Entity Movie

Id (int)

[Include]
MovieLocalizedInformations (EntityCollection<MovieLocalizedInformation>)

Entity MovieLocalizedInformation

Id (int)
Movie_Id (int)
LanguageCode (eg.: en)
Title

On my DomainService object, I expose the following method:

public IQueryable<Movie> GetMovies( string languageCode )
{
  return this.ObjectContext.Movies.Include( "MovieLocalizedInformations" );
}

This works fine. But when I try to add where clause to filter out the localized information based on the language code, only the movies get loaded on the client.

Is there a way to achieve the filtering in one query?

Note: I'm also using the DomainDataSource with paging on the client so the solution needs to work with that.

Any help would be greatly appreciated!

Thanks,

Jacques.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一片旧的回忆 2024-12-15 15:14:29

不确定实体框架,但对于 LinqToSqlDomainService,您可以使用 LoadWith loadOption
包含详细信息实体,然后使用 AssociateWith LoadOption 来过滤详细信息,例如

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Movies>(i => i.MovieLocalizedInformations);
options.AssociateWith<Movies>(i => i.MovieLocalizedInformations.Where(d=> myListOfIds.Contains(d.LocationId)));

Not sure about Enitity Framework but with a LinqToSqlDomainService you use the LoadWith loadOption
to include the details entities and then use the AssociateWith LoadOption to filter the detail e.g

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Movies>(i => i.MovieLocalizedInformations);
options.AssociateWith<Movies>(i => i.MovieLocalizedInformations.Where(d=> myListOfIds.Contains(d.LocationId)));
完美的未来在梦里 2024-12-15 15:14:29

好的,

出于效率原因,我决定使用自定义 DTO 对象来获取本地化信息并展平结果。

但是,当我的自定义 DTO 需要引用另一个自定义本地化 DTO 时,会出现同样的问题。

以下是我如何执行与 ObjectSet 提供的 .Include( "PropertyName" ) 相同的操作:

Entity LocalizedMovieCollection

public class LocalizedMovieCollection
{
  [Key]
  public int Id { get; set; }
  public string Name { get; set; } (the result of a sub query based on the language)

  [Include]
  [Association( "LocalizedMovieCollection_LocalizedMovies", "Id", "MovieCollection_Id" )]
  public IEnumerable<LocalizedMovie> Movies { get; set; }
}

Entity LocalizedMovie

public class LocalizedMovie
{
  [Key]
  public int Id { get; set; }
  public string Name { get; set; } (the result of a sub query based on the language)
  public int MovieCollection_Id { get; set; }

  [Include]
  [Association( "LocalizedMovie_LocalizedMovieCollection", "MovieCollection_Id", "Id", IsForeignKey = true]
  public LocalizedMovieCollection MovieCollection { get; set; }
}

然后,我声明了两个方法:一个返回 LocalizedMovieCollection 的 IQueryable,另一个返回 LocalizedMovie 的 IQueryable。 (注意:必须至少有一个方法返回每种类型的实体,以便在 Silverlight 客户端上自动生成实体)

我的目标是自动加载与电影关联的 MovieCollection,因此获取电影的方法定义是如下所示:

public IQueryable<LocalizedMovie> GetMovies( string languageCode )
{
  return from movie in this.ObjectContext.Movies
         join movieLocalizedInfo in this.ObjectContext.MovieLocalizedInformations
           on movie equals movieLocalizedInfo.Movie
         join movieCollection in this.ObjectContext.MovieCollections
           on movie.MovieCollection equals movieCollection
         join movieCollectionLocalizedInfo in this.ObjectContext.MovieCollectionLocalizedInformations
           on movieCollection equals movieCollectionLocalizedInfo.MovieCollection
         where movieLocalizedInfo.LanguageCode == languageCode && movieCollectionLocalizedInfo.LanguageCode == languageCode
         select new LocalizedMovie()
           {
             Id = movie.Id,
             Name = movieLocalizedInfo.Name
             MovieCollection_Id = movieCollection.Id,
             MovieCollection = new LocalizedMovieCollection(){ Id = movieCollection.Id, Name = movieCollectionLocalizedInfo.Name }
           }
}

当 Silverlight 客户端加载查询时,所有 LocalizedMovies 及其关联的 LocalizedMovieCollections 将被加载到上下文中。

Ok,

For efficiency reason, I decided to go with custom DTO object that fetches the localized information and flatten the result.

But, the same problem occurred when my custom DTO needed to reference another custom localized DTO.

Here is how I came to do the same as the .Include( "PropertyName" ) that the ObjectSet offers:

Entity LocalizedMovieCollection

public class LocalizedMovieCollection
{
  [Key]
  public int Id { get; set; }
  public string Name { get; set; } (the result of a sub query based on the language)

  [Include]
  [Association( "LocalizedMovieCollection_LocalizedMovies", "Id", "MovieCollection_Id" )]
  public IEnumerable<LocalizedMovie> Movies { get; set; }
}

Entity LocalizedMovie

public class LocalizedMovie
{
  [Key]
  public int Id { get; set; }
  public string Name { get; set; } (the result of a sub query based on the language)
  public int MovieCollection_Id { get; set; }

  [Include]
  [Association( "LocalizedMovie_LocalizedMovieCollection", "MovieCollection_Id", "Id", IsForeignKey = true]
  public LocalizedMovieCollection MovieCollection { get; set; }
}

Then, I've declared two methods: One that returns an IQueryable of LocalizedMovieCollection and the other, an IQueryable of LocalizedMovie. (Note: There must be at least one method that returns each type of entity for the entity to get auto-generated on the Silverlight client)

My goal is to automatically load the MovieCollection associated with a Movie so the method definition to get the movies is as follow:

public IQueryable<LocalizedMovie> GetMovies( string languageCode )
{
  return from movie in this.ObjectContext.Movies
         join movieLocalizedInfo in this.ObjectContext.MovieLocalizedInformations
           on movie equals movieLocalizedInfo.Movie
         join movieCollection in this.ObjectContext.MovieCollections
           on movie.MovieCollection equals movieCollection
         join movieCollectionLocalizedInfo in this.ObjectContext.MovieCollectionLocalizedInformations
           on movieCollection equals movieCollectionLocalizedInfo.MovieCollection
         where movieLocalizedInfo.LanguageCode == languageCode && movieCollectionLocalizedInfo.LanguageCode == languageCode
         select new LocalizedMovie()
           {
             Id = movie.Id,
             Name = movieLocalizedInfo.Name
             MovieCollection_Id = movieCollection.Id,
             MovieCollection = new LocalizedMovieCollection(){ Id = movieCollection.Id, Name = movieCollectionLocalizedInfo.Name }
           }
}

When the Silverlight client loads the query, all the LocalizedMovies and their associated LocalizedMovieCollections will be loaded into the context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文