简体   繁体   English

在DB4O中按类型查询

[英]Querying by type in DB4O

How do you pass a class type into a function in C#? 如何将类类型传递给C#中的函数?

As I am getting into db4o and C# I wrote the following function after reading the tutorials: 当我进入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);
    }

There are two ways. 有两种方法。 The first is to explicitly use the Type 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);

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. Jon,Jared和yshuditelu给出的答案使用了很大程度上未使用的DB4o查询机制的查询示例,并且可能在将来被弃用。

The preferred methods of querying on DB4O for .NET is native queries and LINQ. 查询DB4O for .NET的首选方法是本机查询和LINQ。

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

Or alternatively using Linq-to-DB4O: 或者使用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 (eg Pilot) at compile time. 这两项工作都为您提供了编译时的类型(例如Pilot)。 If you don't know the type at compile time, you can instead use a DB4O SODA query: 如果您在编译时不知道类型,则可以改为使用DB4O SODA查询:

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)? 编辑为什么使用LINQ而不是SODA,Query-by-Example(QBE)或Native Query(NQ)? Because LINQ makes it very natural to do query expressions. 因为LINQ使得查询表达式非常自然。 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: LINQ是可组合的,这意味着你可以做这样的事情:

var first20MichaelPilots = michaelPilots.Take(20);

And you'll still get an efficient query executed in DB4O when you iterate over the results. 当您迭代结果时,您仍然可以在DB4O中执行高效查询。 Doing the same in SODA or QBE or NQ is ugly at best. 在SODA或QBE或NQ中做同样的事情是最难的。

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);
}

You can either do it manually by using Type : 您可以使用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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM