0

我有 2 张桌子。一个是用户表,其中包含用户 ID 和 userSelection(另一个表的外键)都是主键,因此用户有多个行。

第二个表包含其主要 id 为 userSelection 的列。

我想从第二个表中检索 userId 具有的所有 userSelection 行。我也想使用 linq lambda 表达式。

我让它在 sql 中工作 jsut 无法将其转换为在 c# 中使用。

Select * From column 
where colID in (
                 select colId from users 
                 where userID = 'someUser')

谢谢

4

2 回答 2

0

假设您使用的是实体框架,那么您真正需要的是内部连接。它看起来像这样:

from c in context.Column
join u in context.Users on c.ColId equals u.ColId
where u.UserId = 'SomeUser'
select c;

作为一个类似于(语法可能缺少某些东西)的lambda(这里没有where子句,但很容易添加)

context.Column.Join( context.Users, u => u.ColId, c => c.ColId).Select
于 2014-08-21T19:16:07.230 回答
0

将此代码更改为两部分

Select * From column 
where colID in (
                 select colId from users 
                 where userID = 'someUser')

获取 colId 列表的第一部分:

var colIds = context.users.Where(x=>x.userID == "someUser").Select(x=>x.colId).ToList();

第二部分得到结果使用WhereList.Contains

// IQueryable result
var result = context.column.Where(x=>colIds.Contains(x.colID));

您可以内联使用它,但我建议将其分为两部分。

于 2018-08-08T09:33:07.283 回答