Group By List<Item>

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)

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

Need Your Help

Vanity URL redirection

php regex apache .htaccess mod-rewrite

I have a setup for vanity URLs that redirects http://localhost/username/ to http://localhost/profile.php?id=username

Solve Gurobi model repeatedly in Python

python optimization iteration gurobi

I need to solve a gurobi model repeatedly (with different variable values each iteration). Rather than rebuild the model each iteration, I have tried setting up the model, then looping through repe...