如何将字符串实现为实际类型?
我正在创建一个类似的投影变量:
var projection = Builders<Items>.Projection
.Include(x => x.Name);
include
方法采用表达式expression&lt; func&lt; tsource,object&gt;&gt;字段
。
我想做的是将一个名为键
的字符串参数转换为x.Name
,能够动态地投影items的其他字段在运行时。
var key = "Name";
var projection = Builders<Items>.Projection
.Include(x => x."key");
I am creating a projection variable like this:
var projection = Builders<Items>.Projection
.Include(x => x.Name);
The Include
method takes an expression Expression<Func<TSource,object>> field
.
What I would like to do is to convert a string parameter named key
for example to x.Name
, to be able to dynamically project other fields of Items
at runtime.
var key = "Name";
var projection = Builders<Items>.Projection
.Include(x => x."key");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如评论作为将来参考的书面答案,您可以应用
String
值,即字段名称。从 <代码> projectDefinition&lt; tdocument&gt; .include&lt; tdocument&gt;() ,它支持
fielddefinition
类型的参数,哪个会从
String
值中执行隐式铸件。As written this answer from the comment as the future reference, you can apply
Provide a
string
value which is the field name.As from
ProjectDefinition<TDocument>.Include<TDocument>()
, it supports with the parameter ofFieldDefinition
type,which
FieldDefinition<TDocument>
would perform an implicit cast from thestring
value.