[英]Linq-to-sql join/where?
I've the following table structures 我有以下表格结构
Users id 用户ID
Types id isBool 类型id isBool
UsersTypes userid types UsersTypes用户ID类型
I want to select all the UserTypes based on id and isBool. 我想基于id和isBool选择所有UserType。
I tried this query 我试过这个查询
var q = from usertype in usertypes
from type in types
where type.isBool == false
where userstypes.user == id
select usertype;
But this did not work as expected. 但这没有按预期工作。 My questions are:
我的问题是:
Your query doesn't join those two tables on any common field: 您的查询不会在任何公共字段上合并这两个表:
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;
There are differences between join and where clauses, depending on how they're used. join和where子句之间有区别,这取决于它们的使用方式。 Either way, using a join is preferred because LINQ-to-SQL will generate an inner join rather than a
hash
cross join (and then filtering based on the where clause). 无论哪种方式,都首选使用联接,因为LINQ-to-SQL会生成内部联接,而不是
哈希
交叉联接(然后基于where子句进行过滤)。
You don't need the parenthesis. 您不需要括号。 You can include them though since they do help readability in some cases.
您可以将它们包括在内,因为它们在某些情况下确实有助于提高可读性。
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.