簡體   English   中英

將兩個表中的數據合並到一個視圖中

[英]Merging data from two tables into one view

我在將來自兩個不同表的數據合並到一個視圖時遇到了一些麻煩。 如果您知道該怎么做,請告訴我。 這就是我正在使用的:

我有四個表:

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

等等...

因此,如果在同一時間段內同一家商店有兩枚銀幣,它們將在表中僅一行顯示,並顯示總數。

我遇到的問題是從一個表中獲取分配,並從另一表中獲取使用。

任何可以提供幫助的人都會很棒。

通常,您必須考慮以下步驟:

  • 按所需的時間范圍和店鋪篩選結果(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();

暫無
暫無

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

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