在 LINQ 中使用强类型对象执行 JOIN

发布于 2024-11-02 14:11:20 字数 616 浏览 0 评论 0原文

我有两个使用实体数据模型生成的对象。这些对象如下所示:

public class Song
{
  public int ID { get; set; }
  public string Title { get; set; }
  public double Duration { get; set; }
}

public class AlbumSongLookup
{
  public int ID { get; set; }
  public int SongID { get; set; }
  public int AlbumID { get; set; }
}

我需要使用 LINQ 获取专辑的 Song 对象。我有专辑ID。目前,我正在尝试:

int albumID = GetAlbumID();
var results = from lookup in context.AlbumSongLookups
              where lookup.AlbumID=albumID
              select lookup;

我知道我需要加入。但我不确定的是,如何使用此 LINQ 查询将结果获取为 Song 对象?

谢谢!

I have two objects that have been generated with an entity data model. The objects look like the following:

public class Song
{
  public int ID { get; set; }
  public string Title { get; set; }
  public double Duration { get; set; }
}

public class AlbumSongLookup
{
  public int ID { get; set; }
  public int SongID { get; set; }
  public int AlbumID { get; set; }
}

I need to get the Song objects for an Album using LINQ. I have the Album ID. Currently, I'm trying:

int albumID = GetAlbumID();
var results = from lookup in context.AlbumSongLookups
              where lookup.AlbumID=albumID
              select lookup;

I understand that I need to do a join. But what I'm not sure about is, how do I get the results to be Song objects with this LINQ query?

Thanks!

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

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

发布评论

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

评论(4

离旧人 2024-11-09 14:11:20

此查询返回您期望的结果吗?

var results = from lookup in context.AlbumSongLookups
              join song in context.Songs on lookup.SongID equals song.ID
              where lookup.AlbumID == albumID
              select song;

我假设这里存在 context.Songs

Does this query return what you're expecting?

var results = from lookup in context.AlbumSongLookups
              join song in context.Songs on lookup.SongID equals song.ID
              where lookup.AlbumID == albumID
              select song;

I'm assuming the existence of context.Songs here.

空心↖ 2024-11-09 14:11:20

这不行吗?

from lookup in context.AlbumSongLookups
from songs in context.Song
where lookup.SongID == songs.ID && lookup.SongID == albumID
select songs

does this not work?

from lookup in context.AlbumSongLookups
from songs in context.Song
where lookup.SongID == songs.ID && lookup.SongID == albumID
select songs
从来不烧饼 2024-11-09 14:11:20

您可以将“导航属性”添加到“歌曲”实体,这样您就可以直接访问特定专辑的相应歌曲。实体框架将为您完成繁重的工作。

在您的情况下,它返回一个查找集合,您需要做的是查找与它们对应的 Song 对象。这可以使用以下方法完成:

int albumID = GetAlbumID();
var results = from lookup in context.AlbumSongLookups
              where lookup.AlbumID=albumID
              select context.Songs.SingleOrDefault(s=>s.SongID==lookup.SongID);

You could add a "Navigation Property" to your "Song" Entity, this way, you'll be able to directly access the corresponding Songs of a particular album. Entity framework will do the heavy lifting for you.

In your case, it returns a collection of lookups, what you need to do is look for the Song objects corresponding to them. this can be done using:

int albumID = GetAlbumID();
var results = from lookup in context.AlbumSongLookups
              where lookup.AlbumID=albumID
              select context.Songs.SingleOrDefault(s=>s.SongID==lookup.SongID);
伴我老 2024-11-09 14:11:20

像这样的东西应该有效:

int albumID = GetAlbumID();
var songIDs = from lookup in contenxt.AlbumSongLookups
              where lookup.AlbumID == albumID
              select lookup.SongID;
var results = from song in context.Songs
              where song.SongID in SongIDs
              select song;

Something like this should work:

int albumID = GetAlbumID();
var songIDs = from lookup in contenxt.AlbumSongLookups
              where lookup.AlbumID == albumID
              select lookup.SongID;
var results = from song in context.Songs
              where song.SongID in SongIDs
              select song;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文