简体   繁体   English

LINQ中的SQL语句转换

[英]SQL statement convert in LINQ

I have a Products table and OrdersDetails table. 我有一个Products表和OrdersDetails表。 I want to display top 5 products by order count. 我想按订单数量显示前5个产品。

This is my SQL statement that I want to convert in LINQ: 这是我要在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

Can somebody help me? 有人能帮助我吗?

EDIT 编辑

I use LINQ to EF in my MVC project. 我在我的MVC项目中使用LINQ to EF。

Product entity: 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 entity: 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; }
}

This is my view model class: 这是我的视图模型类:

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; }
}

Now I try something like this: 现在我尝试这样的事情:

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 };

I want to display top 5 products with my view models. 我想用我的视图模型显示前5个产品。 Maybe I must enter a new property in my model for Count? 也许我必须在我的模型中为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM