在 LINQ 中返回多个聚合列

发布于 2024-12-02 12:47:15 字数 648 浏览 4 评论 0原文

我想将以下 SQL 转换为 LINQ:

SELECT
    (Select count(BidID)) as TotalBidNum,
    (Select sum(Amount)) as TotalBidVal
FROM Bids

我已经尝试过:

from b in _dataContext.Bids
select new { TotalBidVal = b.Sum(p => p.Amount), TotalBidNum = b.Count(p => p.BidId) }

但收到错误“Bids 不包含“Sum”的定义,并且没有扩展方法“Sum”接受类型为“Bids”的第一个参数 我怎样

才能在 LINQ 中做到这一点?

结论

最终答案是:

var ctx = _dataContext.Bids;

var itemsBid = (from b in _dataContext.Bids
               select new { TotalBidVal = ctx.Sum(p => p.Amount), TotalBidNum = ctx.Count() }).First();

I would like to translate the following SQL into LINQ:

SELECT
    (Select count(BidID)) as TotalBidNum,
    (Select sum(Amount)) as TotalBidVal
FROM Bids

I've tried this:

from b in _dataContext.Bids
select new { TotalBidVal = b.Sum(p => p.Amount), TotalBidNum = b.Count(p => p.BidId) }

but get an error "Bids does not contain a definition for "Sum" and no extension method "Sum" accepting a first argument of type "Bids" could be found.

How can I do this in LINQ?

Thanks

CONCLUDING:

The final answer was:

var ctx = _dataContext.Bids;

var itemsBid = (from b in _dataContext.Bids
               select new { TotalBidVal = ctx.Sum(p => p.Amount), TotalBidNum = ctx.Count() }).First();

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

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

发布评论

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

评论(4

一抹微笑 2024-12-09 12:47:15

您可以使用GroupBy 编写此查询。 Lambda表达式如下:

    var itemsBid = db.Bids
                     .GroupBy( i => 1)
                     .Select( g => new
                     {
                          TotalBidVal = g.Sum(item => item.Amount), 
                          TotalBidNum = g.Count(item => item.BidId)
                     });

You can write this query using GroupBy. The Lambda expression is as follows:

    var itemsBid = db.Bids
                     .GroupBy( i => 1)
                     .Select( g => new
                     {
                          TotalBidVal = g.Sum(item => item.Amount), 
                          TotalBidNum = g.Count(item => item.BidId)
                     });
菩提树下叶撕阳。 2024-12-09 12:47:15

你可以试试这个。变量 b 是一个实体(对于每次迭代),而 ctx 是一个实体集,它具有您需要的扩展方法。

var ctx = _dataContext.Bids;

var result = ctx
    .Select( x => new
    {
        TotalBidVal = ctx.Sum  ( p => p.Amount ),
        TotalBidNum = ctx.Count( p => p.BidId  )
    } )
    .First();

You could try this out. The variable b is an entity (for every iteration) while ctx is an entityset which has the extension methods you need.

var ctx = _dataContext.Bids;

var result = ctx
    .Select( x => new
    {
        TotalBidVal = ctx.Sum  ( p => p.Amount ),
        TotalBidNum = ctx.Count( p => p.BidId  )
    } )
    .First();
任谁 2024-12-09 12:47:15

这是斯卡塔格解决方案的替代方案:

(from b in _dataContext.Bids.Take(1)
select new 
{
    TotalBidVal = _dataContext.Bids.Sum(p => p.Amount), 
    TotalBidNum = _dataContext.Bids.Count()
}).Single();

虽然没有真正的理由你不能只是说:

var result = new 
{
    TotalBidVal = _dataContext.Bids.Sum(p => p.Amount), 
    TotalBidNum = _dataContext.Bids.Count()
};

它访问了数据库两次,但它的可读性非常好

here's an alternative to scartag's solution:

(from b in _dataContext.Bids.Take(1)
select new 
{
    TotalBidVal = _dataContext.Bids.Sum(p => p.Amount), 
    TotalBidNum = _dataContext.Bids.Count()
}).Single();

Although there's no real reason you can't just say:

var result = new 
{
    TotalBidVal = _dataContext.Bids.Sum(p => p.Amount), 
    TotalBidNum = _dataContext.Bids.Count()
};

It hits the database twice, but its very readable

樱桃奶球 2024-12-09 12:47:15

您可以使用 聚合子句来完成此操作

Aggregate t In _dataContext.Bids
Into TotalBidNum = Count(BidID),
     TotalBidVal = Sum(Amount)

如果您使用的是 Fx4+ 或 Fx2 的扩展 dll,您还可以通过使用并行性来受益

Aggregate t In _dataContext.Bids.AsParallel

You could do it using the Aggregate Clause.

Aggregate t In _dataContext.Bids
Into TotalBidNum = Count(BidID),
     TotalBidVal = Sum(Amount)

If you're using Fx4+ or an extension dll for Fx2, you could also benfit from parallelism by using

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