LINQ:组合join,group by和select value

时间:2018-03-04 16:52:25

标签: c# linq join linq-to-sql group-by

我有一个结合了连接和组的查询,但是当我尝试加入另一个价格表时我没有得到任何结果。

我试图将所有小时数(db.WorkpaperHours)相加,按工作类型(db.WorkpaperTypes)对它们进行分组,获取该类型单位(db.Units)并获取该工作类型的价格(db.InvoicePrices)。

enter image description here

    var articleList = (from wp in db.Workpapers
    join wh in db.WorkpaperHours on wp.Id equals wh.WorkpaperId
    join wt in db.WorkpaperTypes on wh.WorkType equals wt.Id
    join u in db.Units on wt.Unit equals u.Id
    join ip in db.InvoicePrices on wt.Id equals ip.ArticleId //<--- this line

    where wp.InvoiceId == Id

    group wh by new
    {
        wt.Id,
        wh.WorkType,
        wt.Name,
        wt.Hour,
        wt.Vehicle,
        wt.FixedPrice,

        u.UnitName,
        UnitId = u.Id,
        Price = ip.Price
    }
    into grp
    select new InvoiceArticleList
    {
        Name = grp.Key.Name,
        NewId = grp.Key.WorkType,
        Hours = grp.Sum(p => p.Hours),
        WorkName = grp.Key.Name,

        Price = grp.Key.Price,
        FixedPrice = grp.Key.FixedPrice,


        IsHour = grp.Key.Hour,
        IsVehicle = grp.Key.Vehicle,
        UnitId = grp.Key.UnitId,
        UnitName = grp.Key.UnitName
    });

我的问题是当我试图从具有这些值的表InvoicePrices中获取价格时:

  1. InvoiceId(int)(应与WorkpaperTypes.Id匹配)
  2. ArticleId(int)(应与WorkpaperTypes.Id匹配)
  3. 价格(十进制)(我想要获取的值)
  4. InvoiceArticleList模型

    public class InvoiceArticleList
    {
        public string Name { get; set; }
        public int NewId { get; set; }
        public int Hours { get; set; }
        public string WorkName { get; set; }
    
        public decimal Price { get; set; }
    
        public bool FixedPrice { get; set; }
    
        public bool IsHour { get; set; }
        public bool IsVehicle { get; set; }
    
        public int UnitId { get; set; }
        public string UnitName { get; set; }
    }
    

0 个答案:

没有答案