如何将sql查询转换为linq语句

kyxcudwk  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(447)

我不熟悉linq复杂查询,如何编写以下内容?
三个表:库、批、计划
库:batchid processingquantity
批次:batchid
计划:计划编号数量执行状态日期

  1. SELECT b.ProductionPlanBatchId,
  2. a.ProductionPlanNo,
  3. a.ProductConfigName,
  4. sum(c.ProcessingQuantity) AS 'ProcessingQuantity',
  5. sum(a.Quantity) AS 'Quantity',
  6. a.ExecState,
  7. round(CONVERT(float,sum(c.ProcessingQuantity))/CONVERT(float,sum(a.Quantity)), 2) AS 'Percent',
  8. a.ProcessingCompletionDate
  9. FROM ProductionPlan a,
  10. ProductionPlan_Batch b,
  11. ProductionLibrary c
  12. WHERE a.ProductionPlanId = b.ProductionPlanId
  13. AND b.ProductionPlanBatchId = c.ProductionPlanBatchId
  14. AND a.ExecState <> 'Deleted'
  15. GROUP BY b.ProductionPlanBatchId,
  16. a.ProductionPlanNo,
  17. a.ProductConfigName,
  18. a.ProcessingCompletionDate,
  19. a.ExecState
  20. HAVING round(Convert(float,sum(c.ProcessingQuantity))/Convert(float,sum(a.Quantity)), 2) < 1
  21. ORDER BY b.ProductionPlanBatchId DESC

jgzswidk

jgzswidk1#

根据您的描述,我建议您可以尝试使用下面的linq。
班级:

  1. [Table("ProductionLibrary")]
  2. public partial class ProductionLibrary
  3. {
  4. [Key]
  5. [DatabaseGenerated(DatabaseGeneratedOption.None)]
  6. public int ProductionPlanBatchId { get; set; }
  7. public int? ProcessingQuantity { get; set; }
  8. }
  9. [Table("ProductionPlan")]
  10. public partial class ProductionPlan
  11. {
  12. [Key]
  13. [DatabaseGenerated(DatabaseGeneratedOption.None)]
  14. public int ProductionPlanNo { get; set; }
  15. [StringLength(10)]
  16. public string ProductConfigName { get; set; }
  17. public int? Quantity { get; set; }
  18. [StringLength(10)]
  19. public string ExecState { get; set; }
  20. [StringLength(10)]
  21. public string ProcessingCompletionDate { get; set; }
  22. public int? ProductionPlanId { get; set; }
  23. }
  24. public partial class ProductionPlan_Batch
  25. {
  26. [Key]
  27. [DatabaseGenerated(DatabaseGeneratedOption.None)]
  28. public int ProductionPlanBatchId { get; set; }
  29. public int? ProductionPlanId { get; set; }
  30. }

林克:

  1. var re = from a in dbcontext.ProductionPlans
  2. from b in dbcontext.ProductionPlan_Batch
  3. from c in dbcontext.ProductionLibraries
  4. where a.ProductionPlanId == b.ProductionPlanId
  5. && b.ProductionPlanBatchId == c.ProductionPlanBatchId
  6. && a.ExecState != "Deleted"
  7. select new
  8. {
  9. a.ExecState,
  10. a.ProcessingCompletionDate,
  11. a.ProductConfigName,
  12. a.ProductionPlanId,
  13. a.ProductionPlanNo,
  14. a.Quantity,
  15. b.ProductionPlanBatchId,
  16. bProductionPlanId = b.ProductionPlanId,
  17. c.ProcessingQuantity,
  18. cProductionPlanId = c.ProductionPlanBatchId
  19. }
  20. into p
  21. group p by new {
  22. p.ProductionPlanId,
  23. p.ProductionPlanNo ,
  24. p.ProductConfigName,
  25. p.ProcessingCompletionDate,
  26. p.ExecState,
  27. p.ProductionPlanBatchId
  28. } into grpre
  29. where Math.Round(((decimal)grpre.Sum(x => x.ProcessingQuantity))/((decimal)grpre.Sum(x => x.Quantity)), 2) <1
  30. orderby grpre.Key.ProductionPlanBatchId descending
  31. select new {
  32. grpre.Key.ProductionPlanBatchId,
  33. grpre.Key.ProductionPlanNo,
  34. grpre.Key.ProductConfigName,
  35. ProcessingQuantity = grpre.Sum(x =>x.ProcessingQuantity) ,
  36. Quantity = grpre.Sum(x => x.Quantity),
  37. grpre.Key.ExecState,
  38. Percent = Math.Round(((decimal)grpre.Sum(x => x.ProcessingQuantity)) / ((decimal)grpre.Sum(x => x.Quantity)), 2),
  39. grpre.Key.ProcessingCompletionDate
  40. };
展开查看全部

相关问题