gpt4 book ai didi

c# - Entity Framework Code First - 获取具有特定标签的博客文章

转载 作者:行者123 更新时间:2023-11-30 22:25:37 25 4
gpt4 key购买 nike

我在这里遗漏了一些东西,但我不确定那是什么。所以我需要一双眼睛。我想获得具有与之关联的某些标签的博客文章。我的模型结构如下所示:

public partial class BlogPost : IEntity {

public BlogPost() {

this.BlogPostComments = new HashSet<BlogPostComment>();
this.BlogPostUrls = new HashSet<BlogPostUrl>();
this.TagsForBlogPosts = new HashSet<TagsForBlogPost>();
}

public System.Guid Key { get; set; }
public System.Guid LanguageKey { get; set; }
public Nullable<int> SecondaryKey { get; set; }
public string Title { get; set; }

//...

public virtual ICollection<BlogPostComment> BlogPostComments { get; set; }
public virtual Language Language { get; set; }
public virtual ICollection<BlogPostUrl> BlogPostUrls { get; set; }
public virtual ICollection<TagsForBlogPost> TagsForBlogPosts { get; set; }
}

public partial class Tag : IEntity {

public Tag() {

this.TagsForBlogPosts = new HashSet<TagsForBlogPost>();
this.TagsForDynamicPages = new HashSet<TagsForDynamicPage>();
}

public System.Guid Key { get; set; }
public System.Guid LanguageKey { get; set; }
public string TagName { get; set; }

//...

public virtual Language Language { get; set; }
public virtual ICollection<TagsForBlogPost> TagsForBlogPosts { get; set; }
public virtual ICollection<TagsForDynamicPage> TagsForDynamicPages { get; set; }
}

public partial class TagsForBlogPost : IEntity {

public System.Guid Key { get; set; }
public System.Guid BlogPostKey { get; set; }
public System.Guid TagKey { get; set; }

public virtual BlogPost BlogPost { get; set; }
public virtual Tag Tag { get; set; }
}

作为输入,我有博客文章需要的 string[] 标签。现在,我可以使用以下代码:

public void Get(string[] tags) {

var posts = dbConxtext.BlogPosts.Where(x =>
x.TagsForBlogPosts.Any(y => tags.Contains(y.Tag.TagName)));

//

}

但是它生成了一个 IN 子句,这不是我想要的。这是上面代码生成的 T-SQL:

SELECT 
[Extent1].[Key] AS [Key],
[Extent1].[LanguageKey] AS [LanguageKey],
[Extent1].[SecondaryKey] AS [SecondaryKey],
[Extent1].[Title] AS [Title],
[Extent1].[BriefInfo] AS [BriefInfo],
[Extent1].[Content] AS [Content],
[Extent1].[ImagePath] AS [ImagePath],
[Extent1].[IsApproved] AS [IsApproved],
[Extent1].[CreationIp] AS [CreationIp],
[Extent1].[CreatedOn] AS [CreatedOn],
[Extent1].[LastUpdateIp] AS [LastUpdateIp],
[Extent1].[LastUpdatedOn] AS [LastUpdatedOn]
FROM [dbo].[BlogPosts] AS [Extent1]
WHERE EXISTS (SELECT
1 AS [C1]
FROM [dbo].[TagsForBlogPosts] AS [Extent2]
INNER JOIN [dbo].[Tags] AS [Extent3] ON [Extent2].[TagKey] = [Extent3].[Key]
WHERE ([Extent1].[Key] = [Extent2].[BlogPostKey]) AND ([Extent3].[TagName] IN (N'nuget',N'mvc'))
)

我想要的是与这些标签完全匹配。知道如何实现吗?

编辑:

我需要的是:

帖子A有tag1tag2tag3。帖子 B 有 tag1, tag3。如果字符串数组包含 tag1tag2,则只应选择帖子 A,因为它同时包含 tag1tag2>.

回答:

感谢@msarchet,我设法让它工作。这是 LINQ 查询:

public void Get(string[] tags) {

var posts = dbConxtext.BlogPosts
.Where(x => tags.All(t => x.TagsForBlogPosts.Any(y => y.Tag.TagName == t)));

//

}

并生成 T-SQL:

SELECT 
[Extent1].[Key] AS [Key],
[Extent1].[LanguageKey] AS [LanguageKey],
[Extent1].[SecondaryKey] AS [SecondaryKey],
[Extent1].[Title] AS [Title],
[Extent1].[BriefInfo] AS [BriefInfo],
[Extent1].[Content] AS [Content],
[Extent1].[ImagePath] AS [ImagePath],
[Extent1].[IsApproved] AS [IsApproved],
[Extent1].[CreationIp] AS [CreationIp],
[Extent1].[CreatedOn] AS [CreatedOn],
[Extent1].[LastUpdateIp] AS [LastUpdateIp],
[Extent1].[LastUpdatedOn] AS [LastUpdatedOn]
FROM [dbo].[BlogPosts] AS [Extent1]
WHERE NOT EXISTS (SELECT
1 AS [C1]
FROM (SELECT
N'nuget' AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
UNION ALL
SELECT
N'razor' AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable2]) AS [UnionAll1]
WHERE ( NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[TagsForBlogPosts] AS [Extent2]
INNER JOIN [dbo].[Tags] AS [Extent3] ON [Extent2].[TagKey] = [Extent3].[Key]
WHERE ([Extent1].[Key] = [Extent2].[BlogPostKey]) AND ([Extent3].[TagName] = [UnionAll1].[C1])
)) OR (CASE WHEN ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[TagsForBlogPosts] AS [Extent4]
INNER JOIN [dbo].[Tags] AS [Extent5] ON [Extent4].[TagKey] = [Extent5].[Key]
WHERE ([Extent1].[Key] = [Extent4].[BlogPostKey]) AND ([Extent5].[TagName] = [UnionAll1].[C1])
)) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[TagsForBlogPosts] AS [Extent6]
INNER JOIN [dbo].[Tags] AS [Extent7] ON [Extent6].[TagKey] = [Extent7].[Key]
WHERE ([Extent1].[Key] = [Extent6].[BlogPostKey]) AND ([Extent7].[TagName] = [UnionAll1].[C1])
)) THEN cast(0 as bit) END IS NULL)
)

最佳答案

那么你想根据你的评论做什么

yes. Assuming: the Post A has tag1, tag2 and tag3. The post B has tag1, tag3. If the string array includes tag1 and tag2, the Post A should be selected.

var posts = dbConxtext.BlogPosts.Where(x => tags.All(t => 
x.TagsForBlogPost.Any(y => y.Tag.TagName == t)));

因此请确保所有的 tags 都包含在 TagsForBlogPost

警告,这可能会产生糟糕的 SQL

关于c# - Entity Framework Code First - 获取具有特定标签的博客文章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12188130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com