为什么 LINQ 查询返回的结果/行数比我正在查询的表多?
为了上下文起见,我正在对已导入数据库的多个 Excel 工作表进行 1 次数据转换。数据未标准化,我正在尝试对其进行标准化。
我的 SQL 数据库中有 3 个表。它们被称为 OldAssets、OldTransactions 和 OldUsers。
OldTransactions 有 7903 条记录。 OldUsers 有 7437。OldAssets 有 9764
我正在使用 LINQ to SQL 使用此代码查询这些表
from oa in OldAssets
from ot in OldTransactions
from u in OldUsers
where (oa.Asset_Serial_Number == ot.Asset_Serial_Number && ot.User_EID == u.User_EID && ot.Asset_Tag == oa.Asset_Tag)
select new Transactions {
DevCenter = ot.Transaction_Dev_Center,
Action = ot.Transaction_Action,
Status = ot.Transaction_Status,
ModificationDate = ot.Modified,
ModifiedBy = ot.ModifiedBy,
CreatedBy = ot.CreatedBy,
TransactionDate = (System.DateTime)ot.Transaction_Date,
Transaction_Asset = (System.Int32)oa.ID,
Transaction_User = (System.Int32)u.ID }
我试图遍历所有 OldTransactions,并为每个 OldTransaction 创建一个新事务,根据映射指定 Transaction_Asset 和 Transaction_User资产序列号和 user_EID。
我的结果是给我刚刚超过 10,000 条新“交易”记录。我不明白我如何获得比我拥有的 OldTransactions 数量更多的记录。
我可以编写什么查询来为每个 OldTransaction 返回一个新的 Transaction,但添加了基于 serial_number 和 User_EID 映射的 Transaction_Asset 和 Transaction_User 属性?
For the sake of context, I am working on a 1 time data conversion from several Excel sheets that I have imported into a Database. The data is not normalized and I am trying to normalize it.
I have 3 tables in a SQL Database. They are called OldAssets, OldTransactions, and OldUsers.
OldTransactions has 7903 records. OldUsers has 7437. OldAssets has 9764
I am using LINQ to SQL to query these tables using this code
from oa in OldAssets
from ot in OldTransactions
from u in OldUsers
where (oa.Asset_Serial_Number == ot.Asset_Serial_Number && ot.User_EID == u.User_EID && ot.Asset_Tag == oa.Asset_Tag)
select new Transactions {
DevCenter = ot.Transaction_Dev_Center,
Action = ot.Transaction_Action,
Status = ot.Transaction_Status,
ModificationDate = ot.Modified,
ModifiedBy = ot.ModifiedBy,
CreatedBy = ot.CreatedBy,
TransactionDate = (System.DateTime)ot.Transaction_Date,
Transaction_Asset = (System.Int32)oa.ID,
Transaction_User = (System.Int32)u.ID }
I am trying to go through all of my OldTransactions and for each OldTransaction, create a new transaction that specifies the Transaction_Asset and the Transaction_User based on the mapping of an asset serial number and a user_EID.
My result is giving me just over 10,000 records of new "Transactions." I don't understand how I could be getting more records than the number of OldTransactions I have.
What query can I write that will return a single new Transaction for every OldTransaction, but with the added properties of Transaction_Asset and Transaction_User based on the mapping of serial_number and User_EID?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不应该呢?您从三个表的笛卡尔积中进行选择,这意味着总行数为 7903 x 7437 x 9764,即数十亿:来自 OldAssets 的行、来自 OldTransactions 的行和来自 OldUsers 的行的所有可能组合。
然后这几十亿通过
where
条件过滤,就剩下一万多了,就像你说的那样。Why shouldn't it? You're selecting from the cartesian product of the three tables, meaning that the total number of rows is 7903 x 7437 x 9764, which is several billion: all possible combinations of a row from OldAssets, a row from OldTransactions and a row from OldUsers.
Then these several billion are filtered by the
where
condition, and you are left with just over 10,000, as you said.从您的问题来看,您似乎只想复制带有附加数据的所有交易。我认为这样的事情可以工作:
From you question it looks like you just want to copy all transactions with additional data. I think something like this could work: