[英]Count in select query of Linq to SQL
I am trying to convert below SQL query into Linq. 我正在尝试将下面的SQL查询转换为Linq。
select eg.userid, eg.groupid, count(r.RID) as RecipientCount
from eg
join g on eg.groupid = g.groupid
join r on g.RID = r.RID
where eg.UserId = '7F813844-3B93-418E-8141-654082C4E37D'
and eg.IsDeleted = 0
and r.Isdeleted = 0
group by eg.groupid
Above query runs properly in SQL. 以上查询在SQL中正常运行。
My Linq code is: 我的Linq代码是:
var v = from eml in dc.egs
join recpingroup in dc.g on eml.GroupID equals recpingroup.GroupID
where eml.aspnet_User.LoweredUserName.Equals(strUserName.ToLower())
&& !eml.IsDeleted
&& !recpingroup.r.IsDeleted
select new Info()
{
CreateDt = eml.CreateDt.ToShortDateString(),
UserId = eml.UserId.ToString(),
LastUpdateDt = eml.LastUpdateDt.ToShortDateString(),
Username = eml.aspnet_User.UserName,
GroupDescription = eml.GroupDescription,
GroupID = eml.GroupID.ToString().ToUpper(),
GroupName = eml.GroupName,
Count = dc.g.Count(r1 => r1.GroupID.Equals(eml.GroupID) && !r1.r.IsDeleted)
};
where dc
is my DataContext. dc
是我的DataContext。
But I am having problems in the last property ie Count is coming wrong. 但是我在最后一个属性中遇到问题,即Count错误。 I want the counts of recipients from recpingroup.r as RecipientCount. 我希望来自recpingroup.r的收件人计数为RecipientCount。
Also note that tables are linked in SQL internally by PK and FK references. 还请注意,在SQL中,表是通过PK和FK引用在内部链接的。
尝试跟随
Count = (from x in dc.g where(r1 => r1.GroupID.Equals(eml.GroupID) && !r1.r.IsDeleted) select x.g).count()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.