DB4O中按类型查询
在 C# 中如何将类类型传递给函数?
当我进入 db4o 和 C# 时,我在阅读教程后编写了以下函数:
public static void PrintAllPilots("CLASS HERE", string pathToDb)
{
IObjectContainer db = Db4oFactory.OpenFile(pathToDb);
IObjectSet result = db.QueryByExample(typeof("CLASS HERE"));
db.Close();
ListResult(result);
}
How do you pass a class type into a function in C#?
As I am getting into db4o and C# I wrote the following function after reading the tutorials:
public static void PrintAllPilots("CLASS HERE", string pathToDb)
{
IObjectContainer db = Db4oFactory.OpenFile(pathToDb);
IObjectSet result = db.QueryByExample(typeof("CLASS HERE"));
db.Close();
ListResult(result);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有两种方法。 第一种是显式使用 Type 类型。
第二种是使用泛型
There are two ways. The first is to explicitly use the Type type.
The second is to use generics
Jon、Jared 和 yshuditelu 给出的答案使用了示例查询,这在很大程度上是未使用的 DB4o 查询机制,并且将来可能会被弃用。
在 DB4O for .NET 上查询的首选方法是本机查询和 LINQ。
或者使用 Linq-to-DB4O:
如果您在编译时知道类型(例如 Pilot),这两种方法都可以工作。 如果您在编译时不知道类型,则可以使用 DB4O SODA 查询:
编辑 为什么使用 LINQ 而不是 SODA、按示例查询 (QBE) 或本机查询 (NQ) )? 因为 LINQ 使得查询表达式变得非常自然。 例如,以下是查询名为 Michael 的飞行员的方法:
LINQ 是可组合的,这意味着您可以执行如下操作:
当您迭代结果时,您仍然会在 DB4O 中执行高效的查询。 在 SODA 或 QBE 或 NQ 中做同样的事情充其量是丑陋的。
The answers given by by Jon, Jared, and yshuditelu use query-by-example which is largely unused DB4o querying mechanism, and could potentially be deprecated in the future.
The preferred methods of querying on DB4O for .NET is native queries and LINQ.
Or alternatively using Linq-to-DB4O:
Both of these work provided you know the type (e.g. Pilot) at compile time. If you don't know the type at compile time, you can instead use a DB4O SODA query:
edit Why use LINQ instead of SODA, Query-by-Example (QBE), or Native Query (NQ)? Because LINQ makes it very natural to do query expressions. For example, here's how you'd query for pilots named Michael:
And LINQ is composable, meaning you can do things like this:
And you'll still get an efficient query executed in DB4O when you iterate over the results. Doing the same in SODA or QBE or NQ is ugly at best.
我想这就是你想要的:
I think this is what you want:
您可以使用
Type
手动执行此操作:或者您可以使用泛型来推断类型:
You can either do it manually by using
Type
:Or you could use generics to infer the type: