DB4O中按类型查询

发布于 2024-07-25 08:15:43 字数 354 浏览 6 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔戏命师 2024-08-01 08:15:43

有两种方法。 第一种是显式使用 Type 类型。

public static void PrintAllPilots(Type type, string pathToDb)
{
  ...
  IObjectSet result = db.QueryByExample(type);
}

PrintAllPilots(typeof(SomeType),somePath);

第二种是使用泛型

public static void PrintAllPilots<T>(string pathToDb)
{
  ...
  IObjectSet result = db.QueryByExample(typeof(T));
}

PrintAllPilots<SomeType>(somePath);

There are two ways. The first is to explicitly use the Type type.

public static void PrintAllPilots(Type type, string pathToDb)
{
  ...
  IObjectSet result = db.QueryByExample(type);
}

PrintAllPilots(typeof(SomeType),somePath);

The second is to use generics

public static void PrintAllPilots<T>(string pathToDb)
{
  ...
  IObjectSet result = db.QueryByExample(typeof(T));
}

PrintAllPilots<SomeType>(somePath);
野稚 2024-08-01 08:15:43

Jon、Jared 和 yshuditelu 给出的答案使用了示例查询,这在很大程度上是未使用的 DB4o 查询机制,并且将来可能会被弃用。

在 DB4O for .NET 上查询的首选方法是本机查询和 LINQ。

// Query for all Pilots using DB4O native query:
var result = db.Query<Pilot>();

或者使用 Linq-to-DB4O:

// Query for all Pilots using LINQ
var result = from Pilot p in db
             select p;

如果您在编译时知道类型(例如 Pilot),这两种方法都可以工作。 如果您在编译时不知道类型,则可以使用 DB4O SODA 查询:

var query = db.Query();
query.Constrain(someObj.GetType());
var results = query.Execute();

编辑 为什么使用 LINQ 而不是 SODA、按示例查询 (QBE) 或本机查询 (NQ) )? 因为 LINQ 使得查询表达式变得非常自然。 例如,以下是查询名为 Michael 的飞行员的方法:

var michaelPilots = from Pilot p in db
                    where p.Name == "Michael"
                    select p;

LINQ 是可组合的,这意味着您可以执行如下操作:

var first20MichaelPilots = michaelPilots.Take(20);

当您迭代结果时,您仍然会在 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.

// Query for all Pilots using DB4O native query:
var result = db.Query<Pilot>();

Or alternatively using Linq-to-DB4O:

// Query for all Pilots using LINQ
var result = from Pilot p in db
             select p;

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:

var query = db.Query();
query.Constrain(someObj.GetType());
var results = query.Execute();

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:

var michaelPilots = from Pilot p in db
                    where p.Name == "Michael"
                    select p;

And LINQ is composable, meaning you can do things like this:

var first20MichaelPilots = michaelPilots.Take(20);

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.

ι不睡觉的鱼゛ 2024-08-01 08:15:43

我想这就是你想要的:

public static void PrintAllPilots(Type classType, string pathToDb)
{
    IObjectContainer db = Db4oFactory.OpenFile(pathToDb);
    IObjectSet result = db.QueryByExample(classType);
    db.Close();
    ListResult(result);
}

I think this is what you want:

public static void PrintAllPilots(Type classType, string pathToDb)
{
    IObjectContainer db = Db4oFactory.OpenFile(pathToDb);
    IObjectSet result = db.QueryByExample(classType);
    db.Close();
    ListResult(result);
}
腹黑女流氓 2024-08-01 08:15:43

您可以使用 Type 手动执行此操作:

public static void PrintAllPilots(Type type, string pathToDb)

或者您可以使用泛型来推断类型:

public static void PrintAllPilots<T>(string pathToDb)
{
   //...
   var result = db.QueryByExample(typeof(T));
}

You can either do it manually by using Type:

public static void PrintAllPilots(Type type, string pathToDb)

Or you could use generics to infer the type:

public static void PrintAllPilots<T>(string pathToDb)
{
   //...
   var result = db.QueryByExample(typeof(T));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文