ste*_*776 7 c# asp.net asp.net-mvc entity-framework entity-framework-4
在我的数据库中,我有以下表格:
Person-InterestTag和Post-InterestTag之间存在许多关系
我需要在EF 4.1中执行linq查询以撤回包含至少一个与至少一个与给定用户相关的兴趣标签匹配的兴趣标签的任何帖子.
例
一个人有以下兴趣:
我需要退回任何与汽车,运动或健身相关的帖子.
在性能方面编写此查询的最有效方法是什么?
编辑
根据下面给出的答案遇到错误...
这编译得很好但在运行时抛出错误:
var matchingPosts = posts.Where(post => post.Topics.Any(postTopic => person.Interests.Contains(postTopic)));
Run Code Online (Sandbox Code Playgroud)
错误是:
Unable to create a constant value of type 'System.Collections.Generic.ICollection`1'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
Run Code Online (Sandbox Code Playgroud)
任何想法如何解决这一问题?
编辑2
所以我的课程结构如下:
public class Person
{
public int PersonID {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
//other properties of types string, int, DateTime, etc.
public ICollection<InterestTag> InterestTags {get; set;}
}
public class Post
{
public int PostID {get; set;}
public string Title{get; set;}
public string Content {get; set;}
//other properties of types string, int, DateTime, etc.
public ICollection<InterestTag> InterestTags {get; set;}
}
public class InterestTag
{
public int InterestTagID { get; set; }
public string InterestDescription { get; set; }
public bool Active { get; set; }
public ICollection<Person> Persons { get; set; }
public ICollection<Post> Posts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的Context类中,我重写了OnModelCreating来定义我的数据库表名
modelBuilder.Entity<Person>().HasMany(u => u.InterestTags).WithMany(t => t.Persons)
.Map(m =>
{
m.MapLeftKey("PersonID");
m.MapRightKey("InterestTagID");
m.ToTable("PersonInterestTags");
});
modelBuilder.Entity<Post>().HasMany(u => u.InterestTags).WithMany(t => t.Posts)
.Map(m =>
{
m.MapLeftKey("PostID");
m.MapRightKey("InterestTagID");
m.ToTable("PostInterestTags");
});
Run Code Online (Sandbox Code Playgroud)
在我的查询方法中,我带回了一个IQueryable of Post并应用了一些过滤器,包括我试图在这个问题中完成的子句.
var person = personRepository.Get(x => x.PersonID = 5);
var posts = postRepository.GetQueryable();
//I have tried this and get the error above
posts= posts.Where(x => x.InterestTags.Any(tag => person.InterestTags.Contains(tag)));
Run Code Online (Sandbox Code Playgroud)
personId如果您只从给定的(或)开始,userId您可以在一次往返中执行此查询,如下所示:
var posts = context.Posts
.Intersect(context.People
.Where(p => p.Id == givenPersonId)
.SelectMany(p => p.InterestTags.SelectMany(t => t.Posts)))
.ToList();
Run Code Online (Sandbox Code Playgroud)
这会转换为INTERSECTSQL 语句。
您也可以在两次往返中执行此操作:
var interestTagsOfPerson = context.People.Where(p => p.Id == givenPersonId)
.Select(p => p.InterestTags.Select(t => t.Id))
.SingleOrDefault();
// Result is an IEnumerable<int> which contains the Id of the tags of this person
var posts = context.Posts
.Where(p => p.InterestTags.Any(t => interestTagsOfPerson.Contains(t.Id)))
.ToList();
// Contains translates into an IN clause in SQL
Run Code Online (Sandbox Code Playgroud)
在第二个查询中使用基本类型列表(interestTagsOfPerson是 的集合int)也可以修复您在问题中的编辑中提到的错误。因为Contains您不能在 LINQ to Entities 中使用对象引用,因为 EF 不知道如何将其转换为 SQL。
我不知道这两种方法中哪一种更快(SQL 专家可能有更好的想法),但可能会开始测试第一个选项。(我测试了一下,似乎返回了正确的结果,但这是我第一次使用Intersect。)
编辑
要了解生成的 SQL(从 SQL Profiler 捕获):
第一个查询(使用Intersect)创建以下 SQL 查询:
SELECT
[Intersect1].[Id] AS [C1],
[Intersect1].[Name] AS [C2],
FROM (SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
FROM [dbo].[Posts] AS [Extent1]
INTERSECT
SELECT
[Join1].[Id] AS [Id],
[Join1].[Name] AS [Name],
FROM [dbo].[PersonInterestTags] AS [Extent2]
INNER JOIN (SELECT [Extent3].[TagId] AS [TagId],
[Extent4].[Id] AS [Id],
[Extent4].[Name] AS [Name]
FROM [dbo].[PostInterestTags] AS [Extent3]
INNER JOIN [dbo].[Posts] AS [Extent4]
ON [Extent3].[PostId] = [Extent4].[Id] ) AS [Join1]
ON [Extent2].[TagId] = [Join1].[TagId]
WHERE 1 = [Extent2].[PersonId]) AS [Intersect1]
Run Code Online (Sandbox Code Playgroud)
第二个选项:
Query1(用于人员标签 ID 列表):
SELECT
[Project1].[Id] AS [Id],
[Project1].[C1] AS [C1],
[Project1].[TagId] AS [TagId]
FROM ( SELECT
[Limit1].[Id] AS [Id],
[Extent2].[TagId] AS [TagId],
CASE WHEN ([Extent2].[PersonId] IS NULL)
THEN CAST(NULL AS int)
ELSE 1
END AS [C1]
FROM (SELECT TOP (2) [Extent1].[Id] AS [Id]
FROM [dbo].[People] AS [Extent1]
WHERE 1 = [Extent1].[Id] ) AS [Limit1]
LEFT OUTER JOIN [dbo].[PersonInterestTags] AS [Extent2]
ON [Limit1].[Id] = [Extent2].[PersonId]
) AS [Project1]
ORDER BY [Project1].[Id] ASC, [Project1].[C1] ASC
Run Code Online (Sandbox Code Playgroud)
最终帖子的查询 2:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
FROM [dbo].[Posts] AS [Extent1]
WHERE EXISTS (SELECT
1 AS [C1]
FROM [dbo].[PostInterestTags] AS [Extent2]
WHERE ([Extent1].[Id] = [Extent2].[PostId]) AND ([Extent2].[TagId] IN (1,2,3))
)
Run Code Online (Sandbox Code Playgroud)
在此示例中,查询 1 返回 (1,2,3),因此IN查询 2 的子句中包含 (1,2,3)。
| 归档时间: |
|
| 查看次数: |
517 次 |
| 最近记录: |