LINQ语法问题

发布于 2024-11-02 16:51:49 字数 573 浏览 1 评论 0原文

我有这个原始 SQL,需要在 LINQ 中重写:

SELECT 
    luProfiles.luProfileID,
    luProfiles.ProfileName,
    NoOfRights = (SELECT Count(pkProfileRightsID) FROM tblProfileRights WHERE fkProfileID = luProfileID)
FROM  luProfiles 
WHERE luProfiles.ProfileName LIKE ...

我已经在 LINQ 中完成了大部分操作,但我不确定如何将 NoOfRights 部分添加到我的 LINQ 中。这就是我到目前为止所做的:

return from p in _database.LuProfiles
       where p.ProfileName.ToLower().StartsWith(strProfile.ToLower())             
       select p; 

任何人都可以告诉我在 LINQ 中包含 NoOfRights 部分的正确语法吗?

I have this original SQL that I need to rewrite in LINQ :

SELECT 
    luProfiles.luProfileID,
    luProfiles.ProfileName,
    NoOfRights = (SELECT Count(pkProfileRightsID) FROM tblProfileRights WHERE fkProfileID = luProfileID)
FROM  luProfiles 
WHERE luProfiles.ProfileName LIKE ...

I have done most of it in LINQ, but I am not sure how to add the NoOfRights part to my LINQ. This is what I have done so far :

return from p in _database.LuProfiles
       where p.ProfileName.ToLower().StartsWith(strProfile.ToLower())             
       select p; 

Can anybody tell me the right syntax to include the NoOfRights part in my LINQ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光匆匆的小流年 2024-11-09 16:51:49
from p in _database.LuProfiles
let NoOfRights = (from r in database.tblProfileRights 
                  where r.fkProfileID == p.luProfileID
                  select r).Count()
where p.ProfileName.ToLower().StartsWith(strProfile.ToLower())             
select new
{
    p.luProfileID,
    p.ProfileName,
    NoOfRights 
};
from p in _database.LuProfiles
let NoOfRights = (from r in database.tblProfileRights 
                  where r.fkProfileID == p.luProfileID
                  select r).Count()
where p.ProfileName.ToLower().StartsWith(strProfile.ToLower())             
select new
{
    p.luProfileID,
    p.ProfileName,
    NoOfRights 
};
深爱不及久伴 2024-11-09 16:51:49

如果您使用 LINQ-to-SQL 或 EF,并且设置了 FK,则应该有一个导航属性 ProfileRights。这种情况,可以这样查询:

from p in _database.LuProfiles
where p.ProfileName.ToLower().StartsWith(strProfile.ToLower())
select new 
{
    p.ProfileId,
    p.ProfileName,
    NoOfRights = p.ProfileRights.Count()
};

If you are using LINQ-to-SQL or EF, and you have an FK set up, you should have a navigational property ProfileRights. Tn that case, you can query this way:

from p in _database.LuProfiles
where p.ProfileName.ToLower().StartsWith(strProfile.ToLower())
select new 
{
    p.ProfileId,
    p.ProfileName,
    NoOfRights = p.ProfileRights.Count()
};
望喜 2024-11-09 16:51:49

我认为这会对你有所帮助:

from l in luProfiles
where l.ProfileName.Contains(something)
select new
{
    l.luProfileID,
    l.ProfileName,
    noOfRights = tblProfileRights.Count(t => t.fkProfileID == l.luProfileID)
}

I think this would help you out:

from l in luProfiles
where l.ProfileName.Contains(something)
select new
{
    l.luProfileID,
    l.ProfileName,
    noOfRights = tblProfileRights.Count(t => t.fkProfileID == l.luProfileID)
}
最冷一天 2024-11-09 16:51:49

我建议你首先将 SQL 更改为如下所示:

SELECT 
  luProfiles.luProfileID,
  luProfiles.ProfileName,
  NoOfRights = COUNT(pkProfileRightsID)
FROM luProfiles
LEFT JOIN tblProfileRights ON fkProfileID = luProfileID
WHERE luProfiles.ProfileName like ...
GROUP BY luProfiles.luProfileID, luProfiles.ProfileName

这样就可以轻松地将其转换为 LINQ:(

return from p in _database.LuProfiles
join o in p.Profiles on p.luProfileID equals o.fkProfileID
group p by new { p.luProfileID, p.ProfileName } into g
select new { g.Key.luProfileID, g.Key.ProfileName , g.Count() }

未测试,所以自己动手)

I would recommend you to change SQL first to something like this:

SELECT 
  luProfiles.luProfileID,
  luProfiles.ProfileName,
  NoOfRights = COUNT(pkProfileRightsID)
FROM luProfiles
LEFT JOIN tblProfileRights ON fkProfileID = luProfileID
WHERE luProfiles.ProfileName like ...
GROUP BY luProfiles.luProfileID, luProfiles.ProfileName

So this can easily be transformed to LINQ:

return from p in _database.LuProfiles
join o in p.Profiles on p.luProfileID equals o.fkProfileID
group p by new { p.luProfileID, p.ProfileName } into g
select new { g.Key.luProfileID, g.Key.ProfileName , g.Count() }

(not tested, so do it yourself)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文