如何使用lambda表达式作为参数

本文关键字:参数 表达式 何使用 lambda | 更新日期: 2023-09-27 18:16:01

我有下面的方法:

private List<TSource> Sort<TSource, TKey>(
    List<TSource> list, 
    Func<TSource, TKey> sorter, 
    SortDirection direction)
{
    ...
}

并且根据情况,参数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;
   }

所以,在这些情况下,你可以观察到,调用总是相同的,除了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);

我不确定这是否可能,但如果是这样,如何实现这一点?我不想重复代码

如何使用lambda表达式作为参数

是的,你可以把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);

先声明lambda变量:

Func<CustomType, string> lambdaExpresion;

switch语句之前。这是可能的,因为两种情况下的lambda类型是相同的;

你可以定义一个变量来保存你的函数,

Func<CustomType, string> lambdaExpresion;

然后将其分配到switch块中,像这样,

switch (sortBy)
{
    case "name":
        lambda = s => s.Name;
        break;
    case "surname":
        lambda = s => s.Surname;
        break;
}

你基本上已经在那里了:

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);

好的,以防没有人使用反射:)

orderedList = this.Sort<CustomType, string>(
                lstCustomTypes,
                s => s.GetType().GetProperty(sortBy).GetValue(s).Tostring(),
                 direction == "ASC" ? SortDirection.Ascending : SortDirection.Descending);

只需注意不存在的道具或字段,不兼容的名称(例如您可能希望首字母大写)。错误检查作为一个练习(因为我现在没有一个框来测试这段代码)

你已经答对了。但是,也许您应该直接使用OrderBy和orderbydescent:

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.orderby.aspx

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.orderbydescending.aspx

来自第一个链接的例子:

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);