小编Mar*_*sen的帖子

使用Java和MongoDB进行参数化查询

你能用Java和MongoDB做参数化查询 - 有点像用JDBC准备好的语句吗?

我想做的就是这样.设置一个采用日期范围的查询 - 然后使用不同的范围调用它.我明白这DBCursor.find(...)不起作用 - 这是一种伪代码来说明我正在寻找的东西.

DBCollection dbc = ...
DBObject pQuery = (DBObject) JSON.parse("{'date' : {'$gte' : ?}, 'date' : {'$lte' : ?}}");
DBCursor aprilResults = dbc.find(pQuery, "2012-04-01", "2012-04-30");
DBCursor mayResults = dbc.find(pQuery, "2012-05-01", "2012-05-31");
...
Run Code Online (Sandbox Code Playgroud)

java api mongodb

5
推荐指数
1
解决办法
4205
查看次数

如何向下转换Generic类型的实例?

我正在努力如何正确使用C#Generics.具体来说,我希望有一个方法,它将Generic Type作为参数,并根据Generic的类型执行不同的操作.但是,我不能"低估"通用类型.见下面的例子.

编译器抱怨演员(Bar<Foo>)说"无法将类型转换Bar<T>Bar<Foo>".但是在运行时,演员阵容很好,因为我已经检查了类型.

public class Foo { }

public class Bar<T> { }

// wraps a Bar of generic type Foo
public class FooBar<T> where T : Foo
{

    Bar<T> bar;

    public FooBar(Bar<T> bar)
    {
        this.bar = bar;
    }

}

public class Thing
{
    public object getTheRightType<T>(Bar<T> bar)
    {
        if (typeof(T) == typeof(Foo))
        {
            return new FooBar<Foo>( (Bar<Foo>) bar);  // won't compile cast
        }
        else
        {
            return bar;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# generics casting type-conversion

1
推荐指数
1
解决办法
3789
查看次数

标签 统计

api ×1

c# ×1

casting ×1

generics ×1

java ×1

mongodb ×1

type-conversion ×1