簡體   English   中英

如何將SQL查詢轉換為LINQ Lambda表達式-Sum,LeftJoin,GroupBy

[英]How can I translate SQL query to LINQ Lambda Expression - Sum, LeftJoin, GroupBy

我已經有此SQL查詢和Linq,如何轉換此SQL查詢並將其添加到Linq Expression中?

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

我使用類對數據庫建模。 嘗試這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM