我是 ASP.NET 的新手,正在尝试编写一个在 SQL 中没有问题但在 C# 中无法执行的查询。
我有两张桌子:
城镇
t_id(主要)
t_desc
TownCounty
tc_id (primary )
co_id (county id )
t_id (from the town table)
t_active (Y/N)
我想要做的是显示所有未分配给特定县(co_id)(即“DUB”)的城镇,如果它们被分配给该县,则在它们不活动时显示它们(t_active =“N” )。
在 SQL 中,我编写了以下语句,效果很好
select a.t_desc as "Town " from town a
where not exists(
select * from towncounty b where
b.co_id like "DUB" and
b.t_active = "Y" and
b.t_id = a.t_id)
现在我正试图让它在 ASP.NET (C#) 中工作。我已经写了以下声明,但无济于事:
IEnumerable<Town> Towns = (from co in handyman.townCounties
where co.co_id != county_id || t_active = "N"
join to in handyman.towns on co.t_id equals to.t_id into coto
from subcoto in coto.DefaultIfEmpty()
select new Town
{
Id = subcoto.t_id, <br/>
Name = subcoto.t_desc <br/>
});
试图做一个左连接,但它返回一个烂摊子。任何帮助,将不胜感激。