[英]SQL statement convert in LINQ
我有一個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輸入一個新屬性?
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);
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.