如何将SQL查询转换为LINQ Lambda表达式-Sum,LeftJoin,GroupBy

时间:2019-07-18 15:07:59

标签: c# sql entity-framework linq lambda

我已经有此SQL查询和Linq,如何转换此SQL查询并将其添加到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查询转换为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};

1 个答案:

答案 0 :(得分:0)

我使用类对数据库建模。尝试这样的事情:

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