按组列表<项目> [英] Group By List<Item>

查看:90
本文介绍了按组列表<项目>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类:

I have this class:

public class Item 
{
    public virtual int32 Id { get; set; }
    public virtual Previa Previa { get; set; }
    public virtual Product Product { get; set; }
    public virtual Int32 Quantity { get; set; }
    public virtual Decimal Price { get; set; }
    public virtual Decimal Total { get; set; }
}

现在我需要查询来查找数据库中的每个项目,组按产品计算,数量和总计之和。
为了找到每件物品,我使用:

And now i need to a query to find every Items in database, group by Products, with the sum of Quantity and Total. For find every Items, i use:

public List<Item> FindItem(int IdProduct = 0)
{
    var retorno = (from c in Session.Query<Item>()
                   select c);

    if (IdProduto > 0)
        retorno = retorno.Where(x => x.Product.Id == IdProduct)

    return retorno.ToList<Item>();
}

但我不知道如何分组这些项目。有人可以帮我吗?

But I don't know how to group these items. Could someone please help me?

推荐答案

你的问题很不清楚,但它听起来像需要一个查询,例如:

Your question is far from clear, but it sounds like you want a query such as:

var query = from item in Session.Query<Item>()
            group item by item.Product into g
            select new {
                Product = g.Key,
                Quantity = g.Sum(item => item.Quantity),
                Total = g.Sum(item => item.Total)
            };

然后你如何传递回来是另一回事 - 几乎可以肯定想要列表< Item> 尽管...

How you then pass that back is a different matter - you almost certainly don't want a List<Item> though...

这篇关于按组列表&lt;项目&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆