【问题标题】:How can I translate SQL query to LINQ Lambda Expression - Sum, LeftJoin, GroupBy如何将 SQL 查询转换为 LINQ Lambda 表达式 - Sum、LeftJoin、GroupBy
【发布时间】:2019-11-27 13:45:26
【问题描述】:

我已经有了这个 SQL Query 和 Linq,如何翻译这个 SQL Query 并添加到 Linq 表达式中?

SELECT [Id]
  ,[Stat]
  ,[Date]
  ,[MasterID]
  ,[Count]
  ,a.[SheetNO]
  ,b.SUM
  FROM [dbo].[Goods] as a left join (SELECT [SheetNO]
  ,SUM(Count) as SUM
  FROM [dbo].[Goods]
  Group by [SheetNO]) as b on a.SheetNO = b.SheetNO

(from m in repoGoods.All().Where(x=> 
x.Date<DbFunctions.AddDays(DateTime.Now, 1)) join n in 
repoGoodsUnit.All() on m.MasterID equals n.Id select new DailyVM() { 
GoodsName = n.GoodsName, Price= n.Price*m.Count, GoodsCount = 
m.Count,SheetNO = m.SheetNO.Value,subtotal= n.Price * m.Count });

我只尝试了将 SQL Query 转换为 LINQ,但它也无法正常工作。

from m in repoGoods.All() join n in 
repoGoods.All().GroupBy(x=>x.SheetNO).Select(x => new { SheetNO = 
x.SheetNO , subtotal = x.Sum(e => e.Count)}) on m.SheetNO equals 
n.SheetNO select new { m.SheetNO,n.subtotal};

【问题讨论】:

  • 感谢您的帮助!但这个解决方案与我想要的略有不同。拜托,看看这张红方图谢谢! imgur.com/drmB00N 并更新 SQL 查询 SELECT [Id] ,[Stat] ,[Date] ,[MasterID] ,[Count] ,a.[SheetNO] ,b.SUM FROM [dbo].[Goods] 作为左连接(SELECT [SheetNO] ,SUM(c.Count*d.Price) as SUM FROM [dbo].[Goods] as c left join dbo.GoodsUnit as d on c.MasterID=d.Id Group by [SheetNO]) as b on a.SheetNO = b.SheetNO

标签: c# sql entity-framework linq lambda


【解决方案1】:

我使用类对数据库进行建模。试试这样的:

   class Program
    {
        static void Main(string[] args)
        {
            List<Goods> repoGoods = new List<Goods>();
            List<Unit> repoGoodsUnit = new List<Unit>();

            var results = (from m in repoGoods.Where(x =>
                 x.Date < DbFunctions.AddDays(DateTime.Now, 1))
                           join n in repoGoodsUnit on m.MasterID equals n.Id
                           select new DailyVM()
                           {
                               GoodsName = n.GoodsName,
                               Price = n.Price * m.Count,
                               GoodsCount = m.Count,
                               SheetNO = m.SheetNO,
                               subtotal = n.Price * m.Count,
                               Date = m.Date,
                               MasterID = m.MasterID,

                           })
             .GroupBy(x => new { name = x.GoodsName, sheet = x.SheetNO, date = x.Date, masterId = x.MasterID })
             .Select(x => new DailyVM()
             {
                 GoodsName = x.Key.name,
                 Price = x.Sum(y => y.Price),
                 GoodsCount = x.Sum(y => y.GoodsCount),
                 SheetNO = x.Key.sheet,
                 subtotal = x.Sum(y => y.subtotal),
                 Date = x.Key.date,
                 MasterID = x.Key.masterId
             }).ToList();
        }

    }
    public class DailyVM
    {
        public string GoodsName { get; set; }
        public int Price { get; set; }
        public int GoodsCount { get; set; }
        public int SheetNO { get; set; }
        public int subtotal { get; set; }
        public DateTime Date { get; set; }
        public int MasterID { get; set; }
    }
    public class Goods
    {
        public DateTime Date { get; set; }
        public int MasterID { get; set; }
        public int Count { get; set; }
        public int SheetNO { get; set; }
        public string Stat { get; set; }
    }
    public class Unit
    {
        public int Id { get; set; }
        public string GoodsName { get; set; }
        public int Price { get; set; }
    }
    public static class DbFunctions
    {
        public static DateTime AddDays(DateTime time, int days)
        {
            return DateTime.Now;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    相关资源
    最近更新 更多