[英]Linq-to-sql join/where?
我有以下表格结构
用户ID
类型id isBool
UsersTypes用户ID类型
我想基于id和isBool选择所有UserType。
我试过这个查询
var q = from usertype in usertypes
from type in types
where type.isBool == false
where userstypes.user == id
select usertype;
但这没有按预期工作。 我的问题是:
您的查询不会在任何公共字段上合并这两个表:
var q = from u in usertypes
join t in types on u.typeid equals t.id
where t.isBool == false && usertypes.user == id
select u;
join和where子句之间有区别,这取决于它们的使用方式。 无论哪种方式,都首选使用联接,因为LINQ-to-SQL会生成内部联接,而不是
哈希
交叉联接(然后基于where子句进行过滤)。
您不需要括号。 您可以将它们包括在内,因为它们在某些情况下确实有助于提高可读性。
var q = from usertype in usertypes
from type in types
where type.isBool == false
where usertype.user == id
where usertype.typeid = type.id //join criteria
select usertype;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.