我有一个Products
表和OrdersDetails
表。我想按订单数量显示前5个产品。
这是我要在LINQ中转换的SQL语句:
SELECT TOP 5 P.ProductId, COUNT(OD.OrderId) AS 'Quantity Ordered' FROM Products AS P
INNER JOIN OrdersDetails AS OD
ON P.ProductId = OD.ProductId
GROUP BY P.ProductId
ORDER BY 'Quantity Ordered' DESC
有人能帮助我吗?
修改
我在MVC项目中使用LINQ to EF。
Product
实体:
public partial class Product
{
public Product()
{
this.OrdersDetails = new HashSet<OrdersDetail>();
this.ProductHistories = new HashSet<ProductHistory>();
this.ProductReviews = new HashSet<ProductReview>();
}
public int ProductId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public Nullable<float> Discount { get; set; }
public int Quantity { get; set; }
public string Size { get; set; }
public string Description { get; set; }
public string ProducerName { get; set; }
public string PaymentMethods { get; set; }
public int Category { get; set; }
public bool IsNew { get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
public bool IsEnable { get; set; }
public virtual Category Category1 { get; set; }
public virtual ICollection<OrdersDetail> OrdersDetails { get; set; }
public virtual ICollection<ProductHistory> ProductHistories { get; set; }
public virtual ICollection<ProductReview> ProductReviews { get; set; }
}
OrdersDetail
实体:
public partial class OrdersDetail
{
public int OrderId { get; set; }
public int ProductId { get; set; }
public Nullable<int> Quantity { get; set; }
public Nullable<decimal> Price { get; set; }
public Nullable<float> Discount { get; set; }
public virtual Order Order { get; set; }
public virtual Product Product { get; set; }
}
这是我的视图模型类:
public class ProductsViewModel
{
public int ProductId { get; set; }
[Required(ErrorMessage = "The Product Name is required.")]
public string Name { get; set; }
[Required(ErrorMessage = "The Price is required.")]
[Range(0.01, int.MaxValue, ErrorMessage = "Price must be a positive number.")]
public decimal Price { get; set; }
[Required(ErrorMessage = "The Discount is required.")]
[Range(0, 99.99F, ErrorMessage = "Discount must be betwen 0 and 99,99.")]
public float? Discount { get; set; }
[Required(ErrorMessage = "The Quantity is required.")]
[Range(0, int.MaxValue, ErrorMessage = "Quantity must be a positive number or zero.")]
public int Quantity { get; set; }
public string Size { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
public string ProducerName { get; set; }
public string PaymentMethods { get; set; }
public bool IsNew { get; set; }
public bool IsEnable { get; set; }
public SelectList CategoryList { get; set; }
public string Category { get; set; }
[Required(ErrorMessage = "The Category is required.")]
public int CategoryID { get; set; }
public string Subcategory { get; set; }
public int SubcategoryID { get; set; }
public DateTime? CreateDate { get; set; }
public string ProductImage
{
get { return Name.Replace(" ", string.Empty) + ".jpg"; }
set { }
}
public HttpPostedFileBase UploadedFile { get; set; }
public IEnumerable<ProductHistory> ProductHistory { get; set; }
[Required(ErrorMessage = "The Review is required.")]
[DataType(DataType.MultilineText)]
public string Review { get; set; }
[Required(ErrorMessage = "The Rate is required.")]
[Range(1, 5, ErrorMessage = "Rate must be between 1 and 5")]
public int RateProduct { get; set; }
public IEnumerable<ProductReview> ProductReviews { get; set; }
}
现在我尝试这样的事情:
IEnumerable<ProductsViewModel> product = from pro in context.Products
join order in context.OrdersDetails
on pro.ProductId equals order.ProductId
group pro by pro.ProductId into g
select new ProductsViewModel { //maybe here I try to Count };
我想用我的视图模型显示前5个产品。也许我必须在我的模型中为Count输入一个新属性?
答案 0 :(得分:2)
IEnumerable<ProductsViewModel> product = (from pro in context.Products
join order in context.OrdersDetails
on pro.ProductId equals order.ProductId
group pro by pro.ProductId into g
select new ProductsViewModel).take(5);
答案 1 :(得分:1)
var selct = (from b in context.Products
join ord in entities.OrdersDetails on b.ProductId equals ord.ProductId
select new { ProductID= b.ProductId , QuantityOrdered=context.OrdersDetails.Count() }
).GroupBy(x => x.ProductId);