变量中 Linq 查询的 Lambda 表达式

发布于 2024-11-09 10:47:26 字数 587 浏览 0 评论 0 原文

如何定义要在 linq 查询中用作变量的 lambda 表达式?

例如,当按列表项的不同属性对通用列表进行排序时:

 IList<SampleClass> list = new List<SampleClass>();

 // Populate list
 ...

 list.OrderBy(sampleclass => sampleclass.property1);
 list.OrderBy(sampleclass => sampleclass.property2);

我想在变量中定义 lambda 表达式 (sampleclass => Sampleclass.property1) 并调用:

// ??? define expression in a variable ???
Expression expression = sampleclass => sampleclass.property1;

// Sort list by variable expression
list.OrderBy(expression);

提前致谢 飞鸟

How can I define a lambdaexpression that I want to use in a linq query as a variable?

For example when sorting a generic list by different properties of the listitems:

 IList<SampleClass> list = new List<SampleClass>();

 // Populate list
 ...

 list.OrderBy(sampleclass => sampleclass.property1);
 list.OrderBy(sampleclass => sampleclass.property2);

I would like to define the lambda expression (sampleclass => sampleclass.property1) in a variable and call:

// ??? define expression in a variable ???
Expression expression = sampleclass => sampleclass.property1;

// Sort list by variable expression
list.OrderBy(expression);

Thanks in advance
Tobi

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

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

发布评论

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

评论(6

我不会写诗 2024-11-16 10:47:26

您可以使用 Func 重载之一 (Func< ;T, TResult> 准确地说):

Func<SampleClass, PropertyType> expr = sampleclass => sampleclass.property1;
list.OrderBy(expr);

PropertyType 是存储为 property1 的变量类型示例类。例如,如果是 string,您将使用 Func

You can use one of Func overloads (Func<T, TResult> precisely):

Func<SampleClass, PropertyType> expr = sampleclass => sampleclass.property1;
list.OrderBy(expr);

PropertyType is the type of variable stored as property1 in your SampleClass. If it was for example string, you would use Func<SampleClass, string>.

岁月染过的梦 2024-11-16 10:47:26

定义 Func 如下:

  List<SampleClass> list = new List<SampleClass>();   
  Func<SampleClass, int> expr = (c) => c.SomeProperty;
  _HungerLevel = level;


  class SampleClass
  {
    public int SomeProperty { get; set; }
  }

Define a Func<TSampleClass, TPropertyType> as follows:

  List<SampleClass> list = new List<SampleClass>();   
  Func<SampleClass, int> expr = (c) => c.SomeProperty;
  _HungerLevel = level;


  class SampleClass
  {
    public int SomeProperty { get; set; }
  }
静赏你的温柔 2024-11-16 10:47:26

您可以使用:

Func<SampleClass, int> f = sampleClass => sampleClass.Property1;
list.OrderBy(f);

这假定 Property1 的类型是 int。

You can use:

Func<SampleClass, int> f = sampleClass => sampleClass.Property1;
list.OrderBy(f);

This presumes the type of Property1 is int.

笨笨の傻瓜 2024-11-16 10:47:26

你几乎已经做到了。

参数是从序列中获取项目并给出其键作为结果的任何函数(将在其上完成排序的键)。 lambda 表达式只是此类函数的变体。

这些符号是可能的:

list.OrderBy(sampleclass => sampleclass.property1);

Func<SampleClass,string> getKey = sampleclass => sampleclass.property1;
list.OrderBy(getKey);

string GetKey(SampleClass sampleclass)
{
    return sampleclass.property1;
}

list.OrderBy(GetKey);

(我在这里假设 property1 是一个字符串,但这当然不是必需的!)

You have almost already done it.

The parameter is any function taking an item from the sequence and giving its key as a result (the key on which the ordering will be done). A lambda expression is just a variety of such a function.

These notations are possible :

list.OrderBy(sampleclass => sampleclass.property1);

or

Func<SampleClass,string> getKey = sampleclass => sampleclass.property1;
list.OrderBy(getKey);

or

string GetKey(SampleClass sampleclass)
{
    return sampleclass.property1;
}

list.OrderBy(GetKey);

(I supposed here that property1 was a string but it's of course not a requirement !)

忆悲凉 2024-11-16 10:47:26

就像其他人所说的那样,您可以使用 Func 来存储函数的委托。

如果您想使用 Linq-To-Objects 之外的其他内容,那么您也应该将其包含在表达式中。类似于Expression>

Just like other people said, you can use Func<T, TResult> to store delegate to your function.

If you want to use something else than Linq-To-Objects, then you should enclose this in Expression too. Something like Expression<Func<T, TResult>>.

奈何桥上唱咆哮 2024-11-16 10:47:26

如果您纯粹讨论 LINQ to Objects,则此处不需要 Expression,因为 Enumerable.OrderBy 的参数是 Func 委托:

var list = new List<SampleClass> ();

Func<SampleClass, PropertyType1) orderSelector1 = (obj => obj.Property1); // parentheses for clarity
var sequence1 = list.OrderBy (orderSelector1);

Func<SampleClass, PropertyType2) orderSelector1 = (obj => obj.Property2);
var sequence2 = list.OrderBy (orderSelector2);

如果你想多次赋值,你可以让Func返回object

var list = new List<SampleClass> ();
Func<SampleClass, object> orderSelector;

orderSelector = (obj => obj.Property1);
var sequence1 = list.OrderBy (orderSelector);

orderSelector = (obj => obj.Property2);
var sequence2 = list.OrderBy (orderSelector);

如果你确实想要动态属性选择,即调用OrderBy 指定了属性通过字符串,您将需要表达式此线程中有很多示例,允许您执行以下操作:

var list = new List<SampleClass> ();
var sequence1 = list.OrderBy ("Property1");
var sequence2 = list.OrderBy ("Property2");

If you are talking purely about LINQ to Objects, there's no need for Expressions here because the argument to Enumerable.OrderBy is a Func delegate:

var list = new List<SampleClass> ();

Func<SampleClass, PropertyType1) orderSelector1 = (obj => obj.Property1); // parentheses for clarity
var sequence1 = list.OrderBy (orderSelector1);

Func<SampleClass, PropertyType2) orderSelector1 = (obj => obj.Property2);
var sequence2 = list.OrderBy (orderSelector2);

If you want to assign multiple times, you can make Func return object:

var list = new List<SampleClass> ();
Func<SampleClass, object> orderSelector;

orderSelector = (obj => obj.Property1);
var sequence1 = list.OrderBy (orderSelector);

orderSelector = (obj => obj.Property2);
var sequence2 = list.OrderBy (orderSelector);

If you truly want dynamic property selection, i.e. calling OrderBy with a property specified by a string, you would need Expressions. There are plenty of examples in this thread that allow you to do something like:

var list = new List<SampleClass> ();
var sequence1 = list.OrderBy ("Property1");
var sequence2 = list.OrderBy ("Property2");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文