连接多个表并选择具有多个结果的LinQ to SQL顶部

时间:2018-12-20 16:34:16

标签: c# linq

我有三个表: 类别,产品和折扣。

public class Category
   {
     public string CatName { get; set; }
     public int Id { get; set; }
   }
  public class Product
   {
     public string ProductName { get; set; }
     public int Id { get; set; }
     public int CatId { get; set; }
     public int? OrderDisplay { get; set; }
   }
 public class Discount
   {
     public double Discount { get; set; } 
     public int ProductId { get; set; }
   }

我的预期结果如下:

var result  = new List<Result>(){
   Category,
   List<Product> (Top5),
   Discount
}

尝试:

var query = from m in _catRepository.GetAll() 
                        select new
                        {
                            m.CatName,
                            m.Id, 
                        } into mn
                        join p in _proRepository.GetAll()
                            on mn.Id equals p.CatId 
                            into pr
                        let prj = pr.Take(5)
                        select new 
                        {
                            Pro = prj,
                            Cat = mn
                        };

但是pr.Take(5)在内存中执行,我不想要这个。

我可以使用linq to sql在1个查询中执行吗,有可能吗,或者对我有什么建议?

0 个答案:

没有答案