[英]How to use a lambda expression as a parameter?
I have below method: 我有以下方法:
private List<TSource> Sort<TSource, TKey>(
List<TSource> list,
Func<TSource, TKey> sorter,
SortDirection direction)
{
...
}
and depending on the case, the parameter Func<TSource,TKey>
changes, for example, I have following switch: 并且根据具体情况,参数
Func<TSource,TKey>
发生变化,例如,我有以下开关:
public class CustomType
{
string Name {get; set;}
string Surname {get; set;}
...
}
switch (sortBy)
{
case "name":
orderedList = this.Sort<CustomType, string>(
lstCustomTypes,
s => s.Name.ToString(),
direction == "ASC" ? SortDirection.Ascending : SortDirection.Descending);
break;
case "surname":
orderedList = this.Sort<CustomType, string>(
lstCustomTypes,
s => s.Surname.ToString(),
direction == "ASC" ? SortDirection.Ascending : SortDirection.Descending);
break;
}
so, as you can observe in those cases, the call is always the same except for the lambda parameter s => something
, s => something2
so for no repeat code I would like to the something similar to: 所以,正如你可以在这些情况下观察到的那样,除了lambda参数
s => something
, s => something2
之外,调用总是相同的,所以对于没有重复代码,我想要类似的东西:
switch (sortBy)
{
case "name":
lambdaExpresion = s => s.Name.ToString();
break;
case "surname":
lambdaExpresion= s => s.Surname.ToString();
break;
}
orderedList = this.Sort<CustomType, string>(
lstCustomTypes,
lambdaExpresion,
direction == "ASC" ? SortDirection.Ascending : SortDirection.Descending);
I am not sure if it is possible, but if so how to achieve this? 我不确定是否有可能,但如果是这样,如何实现这一目标? I do not want to repeat code.
我不想重复代码。
Yes, you can just assign the lambda to a variable: 是的,您可以将lambda分配给变量:
Func<CustomType, string> lambda;
switch (sortBy)
{
case "name":
lambda = s => s.Name.ToString();
break;
case "surname":
lambda = s => s.Surname.ToString();
break;
}
orderedList = this.Sort<CustomType, string>(
lstCustomTypes,
lambda,
direction == "ASC" ? SortDirection.Ascending : SortDirection.Descending);
Declare the lambda variable first: 首先声明lambda变量:
Func<CustomType, string> lambdaExpresion;
Before the switch
statement. 在
switch
语句之前。 This is possible because type of lambda in both cases are the same; 这是可能的,因为在两种情况下λ的类型都是相同的; otherwise it would not be possible.
否则就不可能。
You can define a variable to hold your function as such, 您可以定义一个变量来保存您的函数,
Func<CustomType, string> lambdaExpresion;
and then assign it in your switch
block, like this, 然后在你的
switch
块中分配它,就像这样,
switch (sortBy)
{
case "name":
lambda = s => s.Name;
break;
case "surname":
lambda = s => s.Surname;
break;
}
You were basically there already: 你基本上已经在那里:
Func<CustomType, string> lambdaExpresion;
switch (sortBy)
{
case "name":
lambdaExpresion = s => s.Name.ToString();
break;
case "surname":
lambdaExpresion = s => s.Surname.ToString();
break;
case default: //need a default value for it to be definitely assigned.
lambdaExpresion = s => "";
}
orderedList = this.Sort(lstCustomTypes, lambdaExpresion,
direction == "ASC" ? SortDirection.Ascending : SortDirection.Descending);
Ok, just in case nobody uses reflection :) 好的,以防万一没有人使用反射:)
orderedList = this.Sort<CustomType, string>(
lstCustomTypes,
s => s.GetType().GetProperty(sortBy).GetValue(s).Tostring(),
direction == "ASC" ? SortDirection.Ascending : SortDirection.Descending);
Just watch out for the non-existing props or fields, incompatible names (like you might want to capitalize the first letter). 只要注意不存在的道具或字段,不兼容的名称(就像你可能想要把第一个字母大写一样)。 error checking left as an excersice (because I don't have a box to test this code right now)
错误检查留下了一个例外(因为我现在没有一个盒子来测试这段代码)
You already got correct answers. 你已经得到了正确的答案。 However, perhaps you should just use OrderBy and OrderByDescending instead:
但是,也许您应该只使用OrderBy和OrderByDescending:
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.orderby.aspx http://msdn.microsoft.com/en-us/library/system.linq.enumerable.orderby.aspx
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.orderbydescending.aspx http://msdn.microsoft.com/en-us/library/system.linq.enumerable.orderbydescending.aspx
Example from the first link: 第一个链接示例:
Pet[] pets = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
IEnumerable<Pet> query = pets.OrderBy(pet => pet.Age);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.