I have two tables:
User
(
UserId int primary key,
Firstname,
Lastname, ...
info ...
)
Friend
(
User1Id int,
User2Id int,
Status varchar(1), -- '1' is friend, '0' is pending friend request
primary key(User1Id,User2Id)
)
(User1Id, User2Id)
is foreign key that references to [User]
table.
I have this SQL query:
select * from User
where UserId in
(
(select User1Id from Friend where User2Id='@id' and Status='1')
union
(select User2Id from Friend where User1Id='@id' and Status='1')
)
It executes successfully and returns data as expected.
But I'm in trouble with in
when translating from SQL to Linq. I can't use .Contains()
because the data type cannot be converted. How can I translate this?
I tried this Linq code:
var query = (from u in Users
where u.UserId.Contains(
(from f in Friends
where f.User1Id==1 && Status=='1'
select f.User2Id
).Union(from f in Friends
where f.User2Id==1 && Status=='1'
select f.User1Id))
select u );
but I get this error:
.Contains() cannot use with Int and IQuery