【问题标题】:LINQ entity from database view with MANY to MANY relation来自数据库视图的 LINQ 实体,具有 MANY 到 MANY 关系
【发布时间】:2012-01-20 12:59:07
【问题描述】:

我有问题。我有一个多对多关系的数据库视图,它看起来像这样

SELECT TOP (100) PERCENT dbo.PostAdditional.Description, dbo.PostAdditional.Summary,  dbo.PostAdditional.Title, dbo.Post.PostID, dbo.Post.Type, dbo.Tags.TagID, dbo.Tags.TagName, 
FROM dbo.Post 
INNER JOIN dbo.PostAdditional ON dbo.Post.PostID = dbo.PostAdditional.PostID 
INNER JOIN dbo.PostWithTags ON dbo.Post.EventID = dbo.PostWithTags.PostID 
INNER JOIN dbo.Tags ON dbo.PostWithTags.TagID = dbo.Tags.TagID
ORDER BY dbo.Post.StartDate, dbo.Post.PubDate

我想使用 LINQ to SQL 来处理它。当我通常将此视图放在 DBML 设计器中时,它会创建一个实体。但是实体将属性 TagName 作为字符串,我希望它作为标签名称数组(或更好的 List)。当我从数据库中获取数据时,我会为每个单个标签获取一份 Post 实体副本,但我想获取一个包含所有关联标签数组的 Post 实体。

【问题讨论】:

    标签: sql-server asp.net-mvc database linq-to-sql


    【解决方案1】:

    也许这会有所帮助: 我从

    更改了此加入
    INNER JOIN dbo.PostWithTags ON dbo.Post.EventID = dbo.PostWithTags.PostID 
    

    INNER JOIN dbo.PostWithTags ON dbo.Post.PostID = dbo.PostWithTags.PostID 
    

    如果我没听错,这就是你想要的吗?

    var test=
        (
            from p in db.Post
            orderby p.StartDate,p.PubDate
            select new
            {
                p.PostID, 
                p.Type,
                Tags=
                    (
                        from pa in db.PostAdditional
                        join pwt in db.PostWithTags 
                            on pa.PostID equals pwt.PostID
                        join t in db.Tags 
                            on pwt.TagID equals t.TagID
                        where p.PostID == pa.PostID
                        select new
                        {
                            pa.Description,
                            pa.Summary,
                            pa.Title,
                            t.TagID,
                            t.TagName
                        }
                    )
            }
        ).ToList();
    

    其中 db 是数据库上下文

    【讨论】:

    • 是的,我想要你写的作为 LINQ 查询,但作为 SQL 视图,并从中创建 LINQ 实体。
    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多