如何使用用户定义的条件查询对象集合?
抱歉,如果这有点抽象。我正处于发展的早期阶段。
我有两种对象类型:
- 需要存储一系列用户定义条件的对象。
- 与第一个对象中定义的零个或多个条件匹配的对象。
这是一个简单的例子,说明它是如何运行的。
- 用户创建多个类型 2 的对象并将它们添加到集合中。
- 然后用户创建类型 1 的对象并为其分配多个条件。
- 系统使用对象类型 1 中的条件生成类型 2 的对象列表,按每个对象匹配条件的百分比排序。
以下是预期输出的示例:
Conditions List
Quantity >= 2 Object5 100%
Value < 2 Object2 75%
Category = "Blah" Object4 50%
Quality > 5 Object1 25%
Object3 0%
每个条件中的第一个操作数是属性的名称,而第二个操作数是该属性的值。
我怎样才能做到这一点?
我的第一个想法是它看起来类似于查询语言。如果它们是数据库表中的记录,我可以为每个条件拼接一个 SQL 字符串,运行每个查询并计算每个记录 ID 在查询结果中出现的次数。但这些都是普通的旧 C# 对象。我还没有决定使用什么来持久化对象,所以我现在不想把自己束缚在数据库引擎上。有什么建议吗?
Sorry if this is a little abstract. I'm in the early stages of development.
I have two object types:
- An object that needs to store a series of user defined conditions.
- An object that matches zero or more of the conditions defined in the first object.
Here's a simple example of how it would run.
- A user creates several objects of type 2 and adds them to a collection.
- Then the user creates an object of type 1 and assigns several conditions to it.
- The system uses the conditions in object type 1 to generate a list of objects of type 2 sorted by what percentage of the conditions each object matches.
Here is a sample of the expected output:
Conditions List
Quantity >= 2 Object5 100%
Value < 2 Object2 75%
Category = "Blah" Object4 50%
Quality > 5 Object1 25%
Object3 0%
The first operand in each condition is the name of a property while the second operand is the value of that property.
How can I accomplish this?
My first thought is that it looks similar to a query language. If they were records in a DB table I could stitch together an SQL string for each condition, run each query and count the number of times each record id showed up in the query results. But these are plain old C# objects. I haven't decided on what to use for object persistence yet so I'd rather not tie myself to a db engine at this point. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这听起来像是 LINQ 就是您想要使用的。特别是,我会研究 LINQ To Objects 和 LINQ To SQL(如果最终选择持久性存储)。
This sounds like LINQ is what you want to use. In particular, I would look into LINQ To Objects and LINQ To SQL if a persistence store is eventually chosen.
另外,还有一篇关于如何直接执行 SQL 查询 (LINQ to SQL)
also, there is an article on how Directly Execute SQL Queries (LINQ to SQL)
下面是在字符串上使用 LINQ-to-Objects 的示例实现:
回想起来,我认为这个示例也可以与 LINQ-to-SQL 一起使用(对
conditions
数组进行一些调整)。这只是一个简单的例子,但您当然可以进一步扩展这个想法。
Here's an example implementation using LINQ-to-Objects on strings:
In retrospect, I think this example might work with LINQ-to-SQL too (with some tweaks on the
conditions
array).This was just a quick example but you can certainly expand on the idea even more.
您可以使用规范模式的修改版本来执行此操作。从将结果表示为百分比的界面开始:
接下来,创建一个应用任意条件的规范:
现在,创建一个线性组合其他规范结果的规范:
最后,构建一个包含所有所需条件并应用的规范它到
Foo
对象列表:此设计支持任意复杂度的规范。
You can do this with a modified version of the Specification pattern. Start out with an interface that expresses results as percentages:
Next, create a specification which applies any arbitrary condition:
Now, create a specification which linearly combines the results of other specifications:
Finally, build a specification which contains all of your desired conditions and apply it to a list of
Foo
objects:This design supports specifications of arbitrary complexity.