0

我创建了三个表 - Podcast、Category 和 CategoryLink。CategoryLink 表仅包含两列 - PodcastId 和 CategoryId。如本文所述,我已经描述了这些表之间的多对多关系。一切正常,但我不知道如何正确进行查询。现在我通过以下方式做到这一点:

var ps = db.Podcasts.Where(p => p.Status.SysStatus > 0);
if (category_id.HasValue)
    ps = ps.Where(p => p.Categories.Where(c => c.Id == category_id.Value).FirstOrDefault() != null);

它有效,但我认为这有点肮脏:-)

它给了我以下 SQL 查询(我删除了多余的列和字符串):

    SELECT 
[Project2].[Id] AS [Id]
FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
    (SELECT TOP (1) 
        [Extent3].[CategoryId] AS [CategoryId]
        FROM [dbo].[CategoryLink] AS [Extent3]
        WHERE ([Extent1].[Id] = [Extent3].[PodcastId]) AND ([Extent3].[CategoryId] = 1)) AS [C1]
    FROM  [dbo].[Podcast] AS [Extent1]
    INNER JOIN [dbo].[PodcastStatus] AS [Extent2] ON [Extent1].[StatusId] = [Extent2].[Id]
    WHERE [Extent2].[SysStatus] > 0
)  AS [Project2]
WHERE [Project2].[C1] IS NOT NULL

问题是如何进行查询,以便我可以在inner join没有子查询的情况下获得“正常”?

谢谢。

4

1 回答 1

0

我不确定您要完成什么。多对多在对象映射中与多对一没有太大区别,只是可以从双方访问关系:

using System.Data.Entity; // For .Include Extension

// Eager-load Categories
var podcasts = db.Prodcasts.Include(x => x.Categories);
foreach (var p in podcats)
{
    foreach (var c in p.Categories)
    {
    }
}

var categories = db.Categories.Include(x => x.Podcasts);
foreach (var c in categories)
{
    foreach (var p in c.Podcasts)
    {
    }
 }

根据您尝试完成的任务,您可能还想做这样的事情:

from c in Customers
join p in Purchases on c.ID equals p.CustomerID           // first join
join pi in PurchaseItems on p.ID equals pi.PurchaseID     // second join
select new
{
c.Name, p.Description, pi.Detail
}

如果您是 LINQ 新手,我建议您看看LinqPad。它包含很多示例,可以让您了解它的工作原理。

更新

你能详细说明你真正想要得到什么吗?

我的第二个示例中的选择也可以在没有PurchaseItems类的情况下完成:

var q = (from p in db.Prodcasts
        from c in p.Categories // note that it is using "p" instead of db.Categories
        select new 
        {
            Podcast = p.Name,
            CategoryName = c.Name
        }).ToList();
于 2012-05-21T10:49:20.200 回答