linq equivalent of Sql stmt? [modified]
-
Hi, I need help with linq for the follwoing statement. "Select productid,productname, sum(productqty) from product join orders on orders.productid=product.id group by productid,productname order by productname asc" Can i make this select from a listOrderProducts. if it is possible from listorderproducts then my statement should be "select(productid,productname,sum(productqty) from listorderproducts group by productid" I hve a class name product. and want to save the above in the list Thanx
modified on Monday, June 22, 2009 12:59 PM
-
Hi, I need help with linq for the follwoing statement. "Select productid,productname, sum(productqty) from product join orders on orders.productid=product.id group by productid,productname order by productname asc" Can i make this select from a listOrderProducts. if it is possible from listorderproducts then my statement should be "select(productid,productname,sum(productqty) from listorderproducts group by productid" I hve a class name product. and want to save the above in the list Thanx
modified on Monday, June 22, 2009 12:59 PM
I guess that's what you want. Untested code alert :)
var ctx = new Context(); var res = ctx.Products.Select(product => new { product.Id, product.ProductName, OrderedQty = ctx.Orders.Where(order => order.ProductId == product.Id) .Sum(order => order.ProductQty) });
Assuming something like that.
class Product
{
public int Id;
public string ProductName;
}class Order { public int ProductId; public int ProductQty; } class Context { public IEnumerable<Product> Products; public IEnumerable<Order> Orders; }
Eslam Afifi
modified on Tuesday, June 23, 2009 12:21 PM
-
I guess that's what you want. Untested code alert :)
var ctx = new Context(); var res = ctx.Products.Select(product => new { product.Id, product.ProductName, OrderedQty = ctx.Orders.Where(order => order.ProductId == product.Id) .Sum(order => order.ProductQty) });
Assuming something like that.
class Product
{
public int Id;
public string ProductName;
}class Order { public int ProductId; public int ProductQty; } class Context { public IEnumerable<Product> Products; public IEnumerable<Order> Orders; }
Eslam Afifi
modified on Tuesday, June 23, 2009 12:21 PM
-
Glad to help.
Eslam Afifi