【发布时间】:2011-11-23 13:30:28
【问题描述】:
我正在尝试强制 Linq 在两个表之间执行内部连接。我举个例子吧。
CREATE TABLE [dbo].[People] (
[PersonId] [int] NOT NULL,
[Name] [nvarchar](MAX) NOT NULL,
[UpdatedDate] [smalldatetime] NOT NULL
... Other fields ...
)
CREATE TABLE [dbo].[CompanyPositions] (
[CompanyPositionId] [int] NOT NULL,
[CompanyId] [int] NOT NULL,
[PersonId] [int] NOT NULL,
... Other fields ...
)
现在我正在使用不寻常的数据库,因为我无法控制人员从 People 表中丢失,但在 CompanyPositions 中有记录。我想通过加入表格来过滤掉缺少人员的 CompanyPositions。
return (from pos in CompanyPositions
join p in People on pos.PersonId equals p.PersonId
select pos).ToList();
Linq 认为此连接是多余的,并将其从它生成的 SQL 中删除。
SELECT
[Extent1].[CompanyPositionId] AS [CompanyPositionId],
[Extent1].[CompanyId] AS [CompanyId],
....
FROM [dbo].[CompanyPositions] AS [Extent1]
但是在我的情况下它并不是多余的。我可以这样修复它
// The min date check will always be true, here to force linq to perform the inner join
var minDate = DateTimeExtensions.SqlMinSmallDate;
return (from pos in CompanyPositions
join p in People on pos.PersonId equals p.PersonId
where p.UpdatedDate >= minDate
select pos).ToList();
然而,这现在在我的 SQL 中创建了一个不必要的 where 子句。作为最纯粹的我想删除它。任何想法或当前的数据库设计是否束缚了我的双手?
【问题讨论】:
-
你在用什么? LINQ到SQL? LINQ 到实体?还有什么?
-
你的模型有导航属性吗?如果是这样,你可以写类似
where pos.Person != null。 -
我正在使用 LinqToSql,我尝试过 'where pos.Person != null' 和 'p.PersonId != 0' 并且 Linq 删除了它们。在 'p.PersonId != 0' 的情况下,它会将其更改为 'pos.PersonId != 0' 这给我留下了深刻的印象,即使这不是我所追求的。
标签: c# sql linq linq-to-sql