我正在尝试查找数据库中具有完全相同的子记录集的所有记录(如所提供)。听起来不是很清楚,但只是坚持,我会解释一下。
有一个带有“条形码”和“文档”字段的表“条形码”:
Barcode | Document
________|_________
A | ABC
A | CDE
A | EFG
B | XYZ
B | VWX
C | ABC
D | ABC
D | CDE
D | EFG
E | EFG
如果您注意到,条形码 A 和 D 具有完全相同的文档集。条形码 C 和 E 是 A 和 D 的子集
然后我有一组文档进入该函数,假设该集合是ABC,CDE,EFG。这是一个列表。 (条形码信息存储在 SQL Server 中,通过 LINQ to SQL 检索)。对于这组文档,我需要找到所有匹配的条形码:A 和 D。但忽略包含文档子集的条形码:应忽略 C 和 E。
目前,我有递归函数可以遍历所有文档,并按传入文档集的一部分将其过滤掉。这为我提供了匹配集,但也包括子集(如 C 和 E ),然后我过滤掉子集。
我相信这不是问题的最佳解决方案,必须有更优雅的解决方案。但我很难想出任何其他方法来做到这一点。
有什么建议吗?
附注
我希望解释足够清楚,如果有人足够受虐的话,我可以提供我拥有的代码 -)
I'm trying to find all records in database that have exactly the same set of child-records, as provided. Does not sound very clear, but just hang-on, I'll explain.
There is a table Barcode with fields Barcode and Document:
Barcode | Document
________|_________
A | ABC
A | CDE
A | EFG
B | XYZ
B | VWX
C | ABC
D | ABC
D | CDE
D | EFG
E | EFG
If you notice, barcodes A and D have exactly the same set of documents. Barcodes C and E are subsets of A and D
Then I have a set of documents coming in to the function, say the set is ABC, CDE, EFG. This comes in as a List. (Barcodes information is stored in SQL Server, retrieved via LINQ to SQL). For this set of documents I need to find all the matching barcodes: A and D. But ignore barcodes containing subsets of documents: C and E should be ignored.
At the moment I have recursive function that walks through all the documents, and filters them out by parts of the incoming set of documents. This provides me with with matching sets, but also includes subsets (like C and E ), then I filter out subsets.
I believe this is not the best solution for the problem and there must be more elegant solution. But I struggle to think of any other ways to do it.
Any suggestions?
p.s.
I hope the explanation is clear enough, I can provide the code I have, if somebody is masochistic enough -)
发布评论
评论(1)
您可以找到其文档包含在输入列表中的每个条形码,按条形码分组,并选择计数等于输入列表中文档数量的所有条形码。
如果您经常执行此查询并且文档集变化不大,则另一种方法是为每个文档创建条形码集的内存索引(
Dictionary) >
)。然后,当您有一组文档需要查找条形码时,您可以迭代它们,使它们的集合相交。You can find every Barcode whose Document is contained in your list of inputs, group by Barcode, and select all the Barcodes whose count is equal to the number of Documents in your input list.
An alternate method if you will be doing this query often and the set of documents doesn't change much, you can create an in-memory index of the set of barcodes for each document (
Dictionary<Document, ISet<Barcode>>
). Then when you have a set of documents you need to find barcodes for, you can iterate over them, intersecting their sets.