Linq-To-Sql 查询中的不明确调用
我对 Linq-To-SQL 还比较陌生,当我尝试使用连接运算符(相当于 SQL 内部连接)时遇到了问题。
获取用户的所有首选项:
return (from u in DataContext.UserPreference
join p in DataContext.Preference on p.Id equals u.PreferenceId
where u.UserId = userId
select p).ToList();
Visual Studio 告诉我查询中的“连接”运算符是 Enumerable 类和 Queryable 类之间的“不明确调用”。
我有什么想法可以解决这个问题吗?
I'm still a little new to Linq-To-SQL and I'm having a problem when I try to use the join operator (doing the equivalent of an SQL inner join).
Get all the preferences for a user:
return (from u in DataContext.UserPreference
join p in DataContext.Preference on p.Id equals u.PreferenceId
where u.UserId = userId
select p).ToList();
Visual Studio tells me that the "join" operator in the query is an "ambiguous call" between the Enumerable class and the Queryable class.
Any ideas how I can resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的连接表达方式错误 - 您应该首先使用
u
,然后使用p
:基本上,您使用连接左侧的第一个范围变量,然后使用右侧的第二个变量。
通常这种错误是由编译器发现的,它准确地表明出了什么问题。我不知道为什么在这种情况下没有,但无论如何上述应该是解决办法。
Your join is expressed the wrong way round - you should be using
u
first, thenp
:Basically you use the first range variable on the left side of the join, and the second variable on the right side.
Usually this sort of error is spotted by the compiler which suggests exactly what's wrong. I don't know why it didn't in this case, but either way the above should be the fix.
我不同意乔恩的观点。您的查询的第三行中有一个赋值运算符,而不是等于运算符,因此
I'm going to disagree with Jon. You have an assignment operator in the 3rd line your query, and not the equality operator, so instead