IEnumerable.External无法正常工作,那我该怎么办? [英] IEnumerable.Except wont work, so what do I do?
问题描述
我有一个linq to sql数据库.非常简化,我们有3个表格:项目"和用户".有一个称为User_Projects的联接表,将它们联接在一起.
I have a linq to sql database. Very simplified we have 3 tables, Projects and Users. There is a joining table called User_Projects which joins them together.
我已经有了一种为给定用户获取IEnumberable<Project>
的有效方法.
I already have a working method of getting IEnumberable<Project>
for a given user.
from up in User_Projects
select up.Project;
现在,我想获取用户无关参与的项目.我认为IEnumerable的except方法在这里会很好:
Now I want to get the projects the user isn't involved with. I figured the except method of IEnumerable would be pretty good here:
return db.Projects.Except(GetProjects());
那会编译,但是我遇到一个运行时错误:除Contains()运算符外,不能在查询操作器的LINQ to SQL实现中使用本地序列."
That compiles, however I get a runtime error: "Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator."
有什么办法可以解决这个问题?
Is there any way to get around this?
一些观点,但没有答案:p
A few views but no answers :p
我已经尝试过:
IEnumerable<Project> allProjects = db.Projects;
IEnumerable<Project> userProjects = GetProjects();
return allProjects.Except(GetProjects());
我知道它与原始语句基本相同-但现在我没有运行时错误.不幸的是,由于某种原因,它实际上并没有做完剩下的部分,只是返回了所有项目
I know it's essentially the same as the original statement - but now i dont get a runtime error. Unfortunately, it doesn't really do the except part and just returns all the projects, for some reason
推荐答案
Linq to Sql无法理解如何使用任意的内存中对象序列.您需要用关系术语来表示,该术语适用于ID:
Linq to Sql doesn't understand how to work with an arbitrary in-memory sequence of objects. You need to express this in relational terms, which works on IDs:
var userProjectIds =
from project in GetProjects()
select project.ProjectId;
var nonUserProjects =
from project in db.Projects
where !userProjectIds.Contains(project.ProjectId)
select project;
这篇关于IEnumerable.External无法正常工作,那我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!