我是linq的新手并且很难用两个表实现搜索,我有一个正常工作的sql server存储过程,但是我想用linq做它并且它们看起来很不一样不能实现用户数和包含正确,任何人都可以帮助我 这是我试图在linq中实现的我的SQL
@pTeamName Varchar (25) = 'ALL',
@pUserFirstName Varchar (25) = 'ALL'
select
t.TeamId,TeamName,[Description],
COUNT(u.UserId)as UserCount from Team t
left outer join [User] u on u.TeamId=t.TeamId
WHERE
(t.TeamName Like '%'+@pTeamName+'%' OR @pTeamName Like 'ALL')
AND (u.FirstName = @pUserFirstName OR @pUserFirstName Like 'ALL')
AND t.Deleted = 0
group by
TeamName,
[Description],
t.TeamId
这是我到目前为止的linq
from t in Teams
join u in Users on t.TeamId equals u.TeamId
where t.TeamName.Contains("Tester")
select new {t.TeamName,t.Description,u.UserId}
答案 0 :(得分:0)
你可以这样试试,
var result = from t in Teams
join u in Users on t.TeamId equals u.TeamId into j1
where (t.TeamName.Contains("Tester") || t.TeamName.Contains("ALL") ) && (u.FirstName.Contains("username") || u.FirstName.Contains("ALL")) && t.Deleted = 0
from j2 in j1.DefaultIfEmpty()
Group j2 by new
{
t.TeamName,
t.Description,
t.TeamID,
} into gcs
select new {TeamName = gcs.key.TeamName,Description = gcs.Key.Description,Count = gcs.Count(x=>x.UserId)}
未经测试可能需要检查语法错误