我有以下Linq-to-SQL查询。在第5行,我正在尝试从我的Packages表中获取记录的数量,其中满足我的Where语句中列出的条件。我认为除了第5行之外,这段代码中的一切都是正确的。我做错了什么?
var Overage = from s in db.Subscriptions
join p in db.Packages on s.UserID equals p.UserID
where
s.SubscriptionType != "PayAsYouGo" &&
(s.CancelDate == null || s.CancelDate >= day) &&
s.StartDate <= day && p.DateReceived <= day &&
(p.DateShipped == null || p.DateShipped >= day)
let AverageBoxSize = (p.Height * p.Length * p.Width) / 1728
let ActiveBoxCount = p.Count()
select new
{
p,
AverageBoxSize,
ActiveBoxCount,
s.Boxes
};
错误消息是“Foo.Data.Package的未知方法Count()”
编辑以下是我的问题的示例:
订阅表:
UserID | Boxes
001 5
002 25
003 5
Boxes是每个用户在其订阅下被允许的最大数量
套餐表
UserID | PackageID | Height | Length | Width
001 00001 10 10 10
001 00002 10 10 10
001 00003 20 10 10
003 00004 10 20 20
003 00005 10 10 10
所需的查询结果
UserID | Boxes | Box Count | Average Box Size
001 5 3 1,333
003 5 2 2,500
用户002未出现,因为Where子句排除了该用户
答案 0 :(得分:0)
P不是一个集合。它应该是:让ActiveBoxCount = db.Packages.Count()
答案 1 :(得分:0)
根据你对Ryan回答的评论,这可能是你想要的:
var Overage = from s in db.Subscriptions
join p in db.Packages on s.UserID equals p.UserID
where
s.SubscriptionType != "PayAsYouGo" &&
(s.CancelDate == null || s.CancelDate >= day) &&
s.StartDate <= day && p.DateReceived <= day &&
(p.DateShipped == null || p.DateShipped >= day)
select new
{
p,
AverageBoxSize = (p.Height * p.Length * p.Width) / 1728,
ActiveBoxCount = db.Packages.Where(x => x.UserID == p.UserID).Count(),
s.Boxes
}
答案 2 :(得分:0)
最简单的方法是用 -
替换你的Let行Group <columns> By UserId into summary = Group
Aggregate x in summary Into ActiveBoxCount = x.Count