簡體   English   中英

復雜的Linq加入EF6

[英]Complex linq join in EF6

有兩個實體,例如jobsolution 它們每個都有一個日期字段,一個級別字段和一個數量字段。

有必要將它們組合起來,以便它們首先按級別分組,然后按月分組,同時,必須對它們的數量求和。

我嘗試了不同的選擇,但是什么也沒有。 主要的問題是按月份分組並匯總所附表格中的數字。

也就是說,輸出應該是一個序列的求和數字,按級別分組,然后按月份分組。

例如:

var jobs = new List<Job>()
{
new Job { Level = 1, Date = new DateTime(2019, 1, 1), Quantity = 111 },
new Job { Level = 1, Date = new DateTime(2019, 1, 20), Quantity = 222 },
new Job { Level = 2, Date = new DateTime(2019, 2, 1), Quantity = 333 },
new Job { Level = 2, Date = new DateTime(2019, 2, 20), Quantity = 444 }
};

var solutions = new List<Solution>()
{
new Solution { Level = 1, Date = new DateTime(2019, 2, 1), Quantity = 555 },
new Solution { Level = 2, Date = new DateTime(2019, 2, 20), Quantity = 666 },
new Solution { Level = 1, Date = new DateTime(2019, 1, 1), Quantity = 777 },
new Solution { Level = 2, Date = new DateTime(2019, 1, 20), Quantity = 888 }
};

輸出:

  • 1級-> 2019年1月1日-> 1110(111 + 222 + 777)
  • 1級-> 2019年2月1日-> 555
  • 2級-> 2019年1月1日-> 888
  • 2級-> 2019年2月1日-> 1443(333 + 444 + 666)

等等。 是的,所有這些都在EF6中。

嘗試按照下面的方法使用Concat。 我為合並創建一個類。 也可以匿名完成。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication116
{
    class Program
    {
        static void Main(string[] args)
        {
            var jobs = new List<Job>()
            {
                new Job { Level = 1, Date = new DateTime(2019, 1, 1), Quantity = 111 },
                new Job { Level = 1, Date = new DateTime(2019, 1, 20), Quantity = 222 },
                new Job { Level = 2, Date = new DateTime(2019, 2, 1), Quantity = 333 },
                new Job { Level = 2, Date = new DateTime(2019, 2, 20), Quantity = 444 }
            };

            var solutions = new List<Solution>()
            {
                new Solution { Level = 1, Date = new DateTime(2019, 2, 1), Quantity = 555 },
                new Solution { Level = 2, Date = new DateTime(2019, 2, 20), Quantity = 666 },
                new Solution { Level = 1, Date = new DateTime(2019, 1, 1), Quantity = 777 },
                new Solution { Level = 2, Date = new DateTime(2019, 1, 20), Quantity = 888 }
            };


            List<LevelDateQuantity> concat = jobs.Select(x => new LevelDateQuantity() { Date = x.Date, Level = x.Level, Quantity = x.Quantity})
                .Concat( solutions.Select(x => new LevelDateQuantity() { Date = x.Date, Level = x.Level, Quantity = x.Quantity})).ToList();

            List<LevelDateQuantity> results = concat.OrderBy(x => x.Level).ThenBy(x => x.Date)
                .GroupBy(x => new { level = x.Level, date = new DateTime(x.Date.Year, x.Date.Month,1)})
                .Select(x => new LevelDateQuantity() { Level = x.Key.level, Date = x.Key.date, Quantity = x.Sum(y => y.Quantity)})
                .ToList();
        }

    }
    public class LevelDateQuantity
    {
        public int Level { get; set; }
        public DateTime Date { get; set; }
        public int Quantity { get; set; }
    }
    public class Job : LevelDateQuantity
    {
        public int Level { get; set; }
        public DateTime Date { get; set; }
        public int Quantity { get; set; }
    }
    public class Solution : LevelDateQuantity
    {
        public int Level { get; set; }
        public DateTime Date { get; set; }
        public int Quantity { get; set; }
    }
}

誓言,因為我們看不到您的poco類結構,所以我們不知道兩個表是分開的還是與主表具有一對多的關系,因此通過提供的代碼,我可以做到這一點;

var jobs = new List<Job>()
{
new Job { Level = 1, Date = new DateTime(2019, 1, 1), Quantity = 111 },
new Job { Level = 1, Date = new DateTime(2019, 1, 20), Quantity = 222 },
new Job { Level = 2, Date = new DateTime(2019, 2, 1), Quantity = 333 },
new Job { Level = 2, Date = new DateTime(2019, 2, 20), Quantity = 444 }
};

var solutions = new List<Solution>()
{
new Solution { Level = 1, Date = new DateTime(2019, 2, 1), Quantity = 555 },
new Solution { Level = 2, Date = new DateTime(2019, 2, 20), Quantity = 666 },
new Solution { Level = 1, Date = new DateTime(2019, 1, 1), Quantity = 777 },
new Solution { Level = 2, Date = new DateTime(2019, 1, 20), Quantity = 888 }
};


foreach (var sol in solutions)
{
  var jb = new Job();
  jb.Level = sol.Level;
  jb.Date = sol.Date ;
  jb.Quantity= sol.Quantity;

   jobs.Add(jb);
}

 var result = Jobs.GroupBy(x=> new { x.Level, x.Date}).Select(x=> new 
 {
  level = x.Key.Level,
  date = x.Key.Date,
  sumQ = x.Sum(y => y.Quantity )
 });

我沒有測試過代碼,也沒有在編譯器中編寫過代碼,所以除了可能解決您的問題之外,還可能存在一些類型錯誤。

暫無
暫無

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

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