1

我正在尝试在基本 MVC(实验项目)中编写一个函数,应该使用以下(或接近)调用该函数:

datastore = myTable.FetchData({{column1, constraint1}, 
                  {column2, constraint2}, ... 
                  {colN, conN}})

功能用途- 它查询具有传递给它的约束的表。例如,

FetchData({{Me.Fields("Price"), "<100"}, {Me.Fields("Type"), "=Component"}})

最终会产生查询

SELECT * FROM table a WHERE Price < 100 AND Type = "Component"

(查询产生比这更多,涉及定义的关系等等,但这超出了这个问题的范围)

我应该如何编写函数定义来获取这些参数?

`Public Function FetchData( ??? ) As foobar

本质上,它类似于字典,因为它是值对的列表。但是,它必须是非唯一的(例如可以调用它来生成col1 > 1 AND col1 < 5)。还考虑了二维数组列表,但该对的每一半都需要是特定类型 - “键”需要是我的ColumnDefinition对象类型或字符串,“值”应该始终是字符串。

处理这个问题的最佳方法是什么?

奖励问题:将运算符与约束 ( "=component") 合并看起来很难看。关于如何编写函数 def 与运算符分开的任何想法?我尝试了一个枚举,但这使它过于冗长 - 我希望这是一个相当易于使用的库。

4

2 回答 2

2

If you are on .NET 4.0 or higher, suggest trying out the Tuple class to represent your queries. Your code may look like below.

Public Function FetchData(ByVal params As List(Of Tuple(Of ColumnDefinition, Char, String))) As foobar

Tuples are only recommended for API's under your control, where the context is obvious. If this is a public or shared API, suggest creating a named class with appropriate properties (as in Nico Schertler's comment). Then, the code may look like.

Public Function FetchData(ByVal params As List(Of MyContainerClass)) As foobar

Hope this helps.

于 2013-05-16T12:27:25.343 回答
0

或者根据您描述的函数调用的形状,如果您肯定使用字符串,并且它始终是 {"Col", "Constraint"} 那么您可以这样做

Public Function FetchData(ByVal MultiDimensionArray(,) As String)
    'this is then how you could pull the pairs of cols and constraints back out
    'where n is the col constraint pair count up to (n) number
    For n As Integer = 0 To MultiDimensionArray.Length - 1
        Dim Col As String = MultiDimensionArray(0, n)
        Dim Constraint As String = = MultiDimensionArray(1, n)
    Next
End Function
于 2013-05-16T18:27:55.890 回答