如何在字符串中创建C#中的表达式?

发布于 2025-02-12 07:01:31 字数 355 浏览 1 评论 0原文

MongoDB C#驱动程序(或任何其他库)中有一些方法,它们仅期望表达式作为一个参数类似:

query.SortByDescending(x => x.Name) // sort by name for example

我想做的是将字段“名称”作为字符串接收,并且能够在运行时对其进行排序:

var sortBy = "Name";
query.SortByDescending(x => x."SortyBy") // something like this

即如何创建一个表达式为x => x。(sortby的值)

There are methods in MongoDB C# driver (or any other libraries) that expect an expression only as a parameter like:

query.SortByDescending(x => x.Name) // sort by name for example

What I would like to do is to receive the field "Name" as a string and be able to sort by it at runtime:

var sortBy = "Name";
query.SortByDescending(x => x."SortyBy") // something like this

i.e how to create an expression to be x => x.(value of sortBy)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

伴我老 2025-02-19 07:01:31

这个问题基本上的答案与您昨天的问题/a>。

答案本质上仅仅是fieldDefinition< t>是隐式从string隐式铸造的原因,因此您可以在任何地方传递string请参阅fieldDefinition< t>参数。

因此,我们可以使用.sort方法(因为这采用了sortDefinition< t>),并使用builder< t> .sort.sort.descresend.descerding采用fieldDefinition< telement>并返回sortDefinition< t>我们需要:

query = query.Sort(Builders<Items>.Sort.Descending(sortBy));

我假设您的文档类型是items在这里,就像您昨天的问题一样。

请注意,您在这里使用的文本名称必须匹配数据库中的名称,这不一定与文档的属性名称相同。如果您想使其更强大,那么您需要使用表达式树来为所讨论的属性生成表达式。


一种替代解决方案是构建expression&lt; func&lt

private static Expression<Func<TDocument, object>> GetPropertyExpression<TDocument>(string propertyName)
{
    var parameter = Expression.Parameter(typeof(TDocument));
    var property = Expression.Property(parameter, propertyName);
    var castResult = Expression.Convert(property, typeof(object));
    return Expression.Lambda<Func<TDocument, object>>(castResult, parameter);
}

query = query.SortByDescending(GetPropertyExpression<Items>(sortBy));

; query = query.sortbydescending(i =&gt; i.name);

This question basically has the same answer as your question yesterday.

The reason the answer is essentially the same is simply that FieldDefinition<T> is implicitly castable from string, so you can pass a string anywhere you see a FieldDefinition<T> argument.

Therefore, we can use the .Sort method (since that takes a SortDefinition<T>) and use Builders<T>.Sort.Descending which takes a FieldDefinition<TElement> and returns the SortDefinition<T> that we need:

query = query.Sort(Builders<Items>.Sort.Descending(sortBy));

I'm assuming your document type is Items here as it was in your question yesterday.

Note that the textual name you use here has to match the name in the database, which isn't necessarily the same as your document's property name. If you want to make it more robust, then you'll want to use expression trees to produce an expression for the property in question.


An alternative solution would be to build the Expression<Func<T, TElement>> that SortByDescending requires:

private static Expression<Func<TDocument, object>> GetPropertyExpression<TDocument>(string propertyName)
{
    var parameter = Expression.Parameter(typeof(TDocument));
    var property = Expression.Property(parameter, propertyName);
    var castResult = Expression.Convert(property, typeof(object));
    return Expression.Lambda<Func<TDocument, object>>(castResult, parameter);
}

query = query.SortByDescending(GetPropertyExpression<Items>(sortBy));

As far as Mongo is concerned, this is no different to having written query = query.SortByDescending(i => i.Name);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文