翻译可查询< T>回到IMongoQuery [英] Translate Queryable<T> back to IMongoQuery

查看:558
本文介绍了翻译可查询< T>回到IMongoQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code

var q = from e in myCollection.AsQueryable<Entity>() where e.Name == "test" select e;

实际的查询是非常复杂的,我不喜欢使用,而不是LINQ QueryBuilder的建设了。

The actual query is very complex and I don't like building it using QueryBuilder instead of LINQ.

所以我想将其转换回IMongoQuery在myCollection.Group使用()调用,因为没有通过LINQ没有GROUPBY支持。

So I want to convert it back to IMongoQuery to use in myCollection.Group() call since there is no GroupBy support through LINQ.

这可能吗?

推荐答案

编辑答案:

我意识到,已经有摆脱LINQ查询蒙戈查询官方途径(我应该知道!)。你必须垂头丧气的IQueryable的&LT; T>到MongoQueryable&LT; T>来获得访问GetMongoQuery方法:

I realized that there already is an official way to get the Mongo query from a LINQ query (I should have known!). You have to downcast the IQueryable<T> to a MongoQueryable<T> to get access to the GetMongoQuery method:

var linqQuery = from e in collection.AsQueryable<Entity>() where e.Name == "test" select e;
var mongoQuery = ((MongoQueryable<Entity>)linqQuery).GetMongoQuery();

原来的答复:

<击>目前有没有官方支持的方式做到这一点,但在不久的将来,我们也打算可以很容易地找出MongoDB的查询LINQ查询映射到。

At the moment there is no officially supported way to do that, but in the near future we do intend to make it easy to find out what MongoDB query the LINQ query was mapped to.

在您可以使用下面的无证内部方法找出MongoDB的查询LINQ查询短期内被映射到:

In the short term you could use the following undocumented internal methods to find out what MongoDB query the LINQ query is mapped to:

var linqQuery = from e in collection.AsQueryable<Entity>() where e.Name == "test" select e;
var translatedQuery = (SelectQuery)MongoQueryTranslator.Translate(linqQuery);
var mongoQuery = translatedQuery.BuildQuery();

但在某些时候,你可能需要从这些无证方法切换到正式支持的方法(未公开的方法可能会改变或在将来被重新命名)。

这篇关于翻译可查询&LT; T&GT;回到IMongoQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆