简体   繁体   中英

Merging data from two tables into one view

I am having some trouble getting the data from two different tables into one view. If you know how to do this please let me know. This is what I'm working with:

I have four tables:

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

Now, I need iterate through these tables to find coins that are from the same store and the same time frame. Then, I need to combine coins with the same ID, total their allocation amount, and then total the amount that they have used. Last, I need to get them into one view that is set up like this:

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

and so on...

So, if there are two silver coins from the same store during the same time frame, they show up in the table in just one line, with the totals.

The problem I am having is getting the allocated from one table and getting the used from the other table.

Anyone out there who can help will be amazing.

Generally, you have to consider the following steps:

  • Filter your result by desired TimeFrame and Shop (LINQ Where )
  • Select the properties that you are interested in or that are needed for further computations (LINQ Select and SelectMany )
  • Group the results and compute sums (LINQ GroupBy )
  • Join different sub-results, select final properties (LINQ Join and GroupJoin )

There's always more than one way. I imagine that using GroupJoin at some point might be more efficient than what I currently came up with and if you start with Coin instead of separately handling CoinAllocation and CoinUsed , you might get a better structured code depending on the available navigation properties...

The following is what I came up with, which might or might not satisfy your needs - there are some uncertainties in your presented model and criteria.

// 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM