Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous Call In Linq-To-Sql Query

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?

like image 249
Liam Smith Avatar asked Nov 23 '25 04:11

Liam Smith


1 Answers

Your join is expressed the wrong way round - you should be using u first, then p:

return (from u in DataContext.UserPreference
        join p in DataContext.Preference on u.PreferenceId equals p.Id
        where u.UserId == userId
        select p).ToList();

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.

like image 128
Jon Skeet Avatar answered Nov 24 '25 17:11

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!