将两个表中的数据合并到一个视图中
问题描述:
我在将两个不同表中的数据合并到一个视图中时遇到了一些麻烦。如果你知道如何做到这一点,请告诉我。这就是我的工作:将两个表中的数据合并到一个视图中
我有四个表:
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
等等...
所以,如果有在同一时间从同一个储存2个银币框架,它们在表格中以总数显示在一行中。
我遇到的问题是从一个表中获取分配,并从另一个表中获取使用。
任何人谁可以帮助将是惊人的。
答
一般情况下,你必须考虑以下步骤:
- 通过所需的时间框架和店(LINQ
Where
) - 过滤您的结果选择你有兴趣或需要进一步的计算性能(LINQ
Select
和SelectMany
) - 组的结果和计算总和(LINQ
GroupBy
) - 加入不同子的结果,选择最终性质(LINQ
Join
和GroupJoin
)
总有不止一种方法。我想,在某些时候使用GroupJoin
可能比我现在用,如果想出了更高效的你开始而不是单独处理CoinAllocation
和CoinUsed
,你可能会因可用的导航性能得到更好的结构化的代码...
以下是我想出的,这可能会满足您的需求 - 您提交的模型和标准中存在一些不确定性。
// 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();
+0
非常感谢!这真的帮了我,我想出了如何过滤我需要的结果。 – Noalani
发布您到目前为止所尝试的内容,以便我们可以看到您出错的位置。 – CaTs
使用这些属性'硬币名称,分配的金额,使用的金额,剩余'做一个视图模型根据您的条件选择这些字段并通过这个视图模型来查看是否有意义的linq选择查询? – Curiousdev
'Coin'表怎么样?我没有看到它,但它肯定是被引用的...... – grek40