将两个表中的数据合并到一个视图中

时间:2017-04-18 22:39:15

标签: c# asp.net-mvc linq oop

我在将两个不同的表中的数据转换为一个视图时遇到了一些麻烦。如果您知道怎么做,请告诉我。这就是我正在使用的:

我有四张桌子:

public class CoinAllocation
{
    public int CoinAllocationID { get; set; }
    public int? StoreID { get; set; }
    public int? TimeFrameID { get; set; }
    public virtual Store Store { get; set; }
    public virtual TimeFrame TimeFrame { get; set; }
    public virtual List<CoinAllocationItem> CoinAllocationItems { get; set; }
}

public class CoinAllocationItem
{
    public int CoinAllocationItemID { get; set; }
    public int? CoinID { get; set; }
    public int? StoreID { get; set; }
    public int? CoinAllocationID { get; set; }
    public int QuantityAllocated { get; set; }
    public virtual Coin Coin { get; set; }
}

public class CoinUsed
{
    public int CoinUsedID { get; set; }
    public int? TimeFrameID { get; set; }
    public int? StoreID { get; set; }
    public virtual Store Store { get; set; }
    public virtual TimeFrame TimeFrame { get; set; }
    public virtual List<CoinUsedItem> CoinUsedItems { get; set; }
}

public class CoinUsedItem
{
    public int CoinUsedItemID { get; set; }
    public int? CoinUsedID { get; set; }
    public int? CoinID { get; set; }
    public int? QuantityUsed { get; set; }
    public virtual Coin Coin { get; set; }
    public int? StoreID { get; set; }
}

现在,我需要遍历这些表来查找来自同一商店和相同时间范围的硬币。然后,我需要组合具有相同ID的硬币,总分配金额,然后总计他们使用的金额。最后,我需要将它们放入一个设置如下的视图中:

Coin Name      | Amount Allocated | Amount Used | Remaining
silver coin      10                 1              9
gold coin        15                 5              10

依旧......

因此,如果在同一时间段内同一商店有两枚银币,它们只会在一行中显示一行,并显示总数。

我遇到的问题是从一个表中获取分配并从另一个表中获取使用。

任何可以提供帮助的人都会很棒。

1 个答案:

答案 0 :(得分:0)

通常,您必须考虑以下步骤:

  • 按照所需的TimeFrame和Shop(LINQ Where
  • 过滤结果
  • 选择您感兴趣的属性或进一步计算所需的属性(LINQ SelectSelectMany
  • 对结果和计算总和进行分组(LINQ GroupBy
  • 加入不同的子结果,选择最终属性(LINQ JoinGroupJoin

总有不止一种方式。我想在某些时候使用GroupJoin可能比我目前提出的更有效,如果你从Coin开始而不是单独处理CoinAllocationCoinUsed,那么你可能会获得更好的结构化代码,具体取决于可用的导航属性......

以下是我提出的建议,可能会或可能不会满足您的需求 - 您提出的模型和标准存在一些不确定性。

// whatever you search for... this assumes you want coins for one store in one timeframe
int desiredStoreID = 0, desiredTimeFrameID = 0;

var coinUsedSelection = db.CoinUsed
    .Where(x => x.StoreID == desiredStoreID && x.TimeFrameID == desiredTimeFrameID)
    .SelectMany(x => x.CoinUsedItems)
    .GroupBy(x => x.CoinID, x => x.QuantityUsed, (k, v) => new { CoinID = k, QuantityUsedSum = v.Sum() });

var coinAllocationSelection = db.CoinAllocations
    .Where(x => x.StoreID == desiredStoreID && x.TimeFrameID == desiredTimeFrameID)
    .SelectMany(x => x.CoinAllocationItems)
    .GroupBy(x => new { x.CoinID, x.Coin.CoinName }, x => x.QuantityAllocated, (k, v) => new { k.CoinID, k.CoinName, QuantityAllocatedSum = v.Sum() });

var result = coinAllocationSelection.Join(coinUsedSelection, ca => ca.CoinID, cu => cu.CoinID, (ca, cu) => new
    {
        CoinName = ca.CoinName,
        AmountAllocated = ca.QuantityAllocatedSum,
        AmountUsed = cu.QuantityUsedSum,
        Remaining = ca.QuantityAllocatedSum - cu.QuantityUsedSum
    })
    .ToList();