Linq查询涉及使用两个未直接连接的表进行分组

时间:2014-11-21 21:25:46

标签: c# linq entity-framework

在具有这些类的EF模型中:

 class BoxOutput
 {
     public long BoxId { get; set; }
     public virtual Box Box { get; set; }
     public long BoxId { get; set; }
     public long CategoryId { get; set; }
     public virtual Category Category { get; set; }
     public long? ColorId { get; set; }
     public virtual Category Color { get; set; }
     public decimal Weight { get; set; }

     // ...and other irrelevant properties
 }

 class BoxInput
 {
     public long BoxId { get; set; }
     public virtual Box Box { get; set; }
     public long BoxId { get; set; }
     public long CategoryId { get; set; }
     public virtual Category Category { get; set; }
     public long? ColorId { get; set; }
     public virtual Category Color { get; set; }
     public decimal Weight { get; set; }

     // ...and other irrelevant properties
 }


...我怎么能做一个LINQ查询,对于特定的框(例如boxId = 12),返回这个?:

  category.name    color.name      inputWeightsSum      outputWeightsSum
 ---------------------------------------------------------------------------
     c             null               0                    0
     c             red                0                    0
     c             blue               0                    0
     m             null               0                    0
     m             red                0                    0
     m             blue               0                    0
                           ....

我目前几乎实现了这一点,但“可选颜色”让我在做“笛卡尔产品”时遇到麻烦:我没有显示空值......

这就是我所拥有的。

var boxesInputs = dbContext.BoxesInputs
.Where(c => c.BoxId == 12)
.GroupBy(c => new { c.CategoryId, c.ColorId })
.Select(g => new
{
    categoryId = g.Key.CategoryId,
    colorId = g.Key.ColorId,
    sumOfWeights = g.Sum(r => (decimal?)r.Weight) ?? 0,
}).ToList();

var boxesOutputs = dbContext.BoxesOutputs
.Where(c => c.BoxId == 12)
.GroupBy(c => new { c.CategoryId, c.ColorId })
.Select(g => new
{
    categoryId = g.Key.CategoryId,
    colorId = g.Key.ColorId,
    sumOfWeights = g.Sum(r => (decimal?)r.Weight) ?? 0,
}).ToList();

var categoriesAndColors = dbContext.Categories.AsEnumerable()
.SelectMany(category => dbContext.Colors.AsEnumerable()
.Select(color => new
{
    category = new
    {
        categoryId = category.CategoryId,
        name = category.Name,
    },
    color = new
    {
        colorId = color.ColorId,
        name = color.Name,
    },
    inputWeightsSum = boxesInputs.Where(r => r.categoryId == category.CategoryId && r.colorId == color.ColorId).Sum(r => (decimal?) r.sumOfWeights) ?? 0,
    outputWeightsSum = boxesOutputs.Where(r => r.categoryId == category.CategoryId && r.colorId == color.ColorId).Sum(r => (decimal?)r.sumOfWeights) ?? 0,
})).ToList();

在前面的代码中,前两个查询返回:

  category.name    color.name      inputWeightsSum
 -------------------------------------------------------
     c             null               0
     c             red                0
     c             blue               0
     m             null               0
     m             red                0
     m             blue               0 
                           ....

  category.name    color.name      outputWeightsSum
 -------------------------------------------------------
     c             null               0
     c             red                0
     c             blue               0
     m             null               0
     m             red                0
     m             blue               0 
                           ....

第三个加入了这两个。我想我需要改进那个连接,因为我正在丢失空值。

此代码也使用内存代码,我希望它是一个sql-query(linq-to-entities,而不是与linq-to-entities混合的linq-to-objects)。可能吗?给BoxOutput和BoxInput是两个不同的表,它们没有直接连接。或者我最终会在最后得到3个查询?

1 个答案:

答案 0 :(得分:0)

您从两个表中选择一个相同的匿名类型,将它们合并在一起,然后执行您的组并选择。像这样:

dbContext.BoxesInputs
.Select(g => new TempClass
{
    CategoryId = g.CategoryId,
    ColorId = g.ColorId,
    InputWeight = r.Weight,
    OutputWeight = 0,
})
.Union(dbContext.BoxesOutputs
    .Select(g => new TempClass
    {
        CategoryId = g.CategoryId,
        ColorId = g.ColorId,
        InputWeight = 0,
        OutputWeight = r.Weight
    })
)
.GroupBy(c => new { c.CategoryId, c.ColorId })
.Select(g => new
{
    categoryId = g.Key.CategoryId,
    colorId = g.Key.ColorId,
    sumOfInputWeights = g.Sum(r => r.InputWeight),
    sumOfOutputWeights = g.Sum(r => r.OutputWeight),
}).ToList();

public class TempClass
{
    public int CategoryID { get; set; }
    public int ColorID { get; set; }
    public decimal InnerWeight { get; set; }
    public decimal OuterWeight { get; set; }
}