我正在使用Csharp Linq创建以下报告
我有两张表格如下
#Users nid pid name 1 1 name1 2 1 name2 #Transactions nid tid location dcode 1 T1 L1 D1 2 T1 L2 D1 2 T2 L1 D1 2 T2 L3 D1
报告包含
a) columns from users table where nid != pid b) columns from transactions where tid == T2 and nid = results from a) c) the combination can have only one top row in result nid name tid Location 2 name2 T2 L1 the second record will not be present - 2 name2 T2 L3
我使用了
尝试了以下内容var report = (from u in users where u.nid != u.pid
join t in transactions
where t.tid == "T2"
on u.nid equals t.nid
select new
{
// the report columns
}).Distinct().ToList();
在第二个'where'显示错误
感谢您的帮助
答案 0 :(得分:2)
交换过滤并加入部分查询,并将tid
重命名为t.tid
或其他所需的过滤子句(在您的示例中,结果表中的事务与tid == "T1"
有交易,但您尝试过滤与T2
):
var report = (from u in users
join t in transactions
on u.nid equals t.tid //<-- this line should precede
where t.tid == "T2" //<-- this one
&& u.nid != u.pid
select new
{
// the report columns
}).Distinct().ToList();
加入部分无法分开,因此在使用where
子句完成join
之前,您无法编写on
。