我需要一个返回keySelector的方法,该方法可以在调用OrderBy表达式时使用。问题是keySelector是泛型委托,我希望该方法在TKey上返回具有不同数据类型的keySelector:
Func<TSource,TKey> keySelector
我想完成此操作,但不是使用lambda表达式,而是一种返回keySelector(TKey = string或int)的方法:
IEnumerable<Person> orderedByFirstName = persons.OrderBy(p => p.FirstName);
IEnumerable<Person> orderedByAge = persons.OrderBy(p => p.Age);
是否可以通过某种方式在TKey上返回具有不同类型的keySelector的方法?不可能返回lambda表达式,但是类似的东西...
只要该方法仅返回数据类型为string的keySelector,一切正常:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
public class Behavior
{
public virtual string GetOrderByKeySelector(Person p)
{
return p.FirstName;
}
}
public class SpecializedBehavior : Behavior
{
public override string GetOrderByKeySelector(Person p)
{
return p.LastName;
}
}
List<Person> persons = new List<Person>
{
new Person{Age=10, FirstName="Pelle", LastName="Larsson"},
new Person{Age=90, FirstName="Nils", LastName="Nilsson"},
new Person{Age=15, FirstName="Olle", LastName="Johansson"},
new Person{Age=30, FirstName="Kalle", LastName="Svensson"}
};
像这样使用:
SpecializedBehavior behavior = new SpecializedBehavior();
IEnumerable<Person> orderedResult = persons.OrderBy(behavior.GetOrderByKeySelector);
答案 0 :(得分:1)
如何将集合带入方法。
void Main()
{
SpecializedBehavior behavior = new SpecializedBehavior();
IEnumerable<Person> orderedResult = behavior.Sort(persons);
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
public class Behavior
{
public virtual IEnumerable<Person> Sort(IEnumerable<Person> persons)
{
return persons.OrderBy(p => p.FirstName);
}
}
public class SpecializedBehavior : Behavior
{
public override IEnumerable<Person> Sort(IEnumerable<Person> persons)
{
return persons.OrderBy(p => p.Age);
}
}
List<Person> persons = new List<Person>
{
new Person{Age=10, FirstName="Pelle", LastName="Larsson"},
new Person{Age=90, FirstName="Nils", LastName="Nilsson"},
new Person{Age=15, FirstName="Olle", LastName="Johansson"},
new Person{Age=30, FirstName="Kalle", LastName="Svensson"}
};
答案 1 :(得分:0)
这个答案只是为了说明@LasseVågsætherKarlsen的评论,所有功劳应该归功于他:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
public class PersonByFirstNameComparer : IComparer<Person>
{
public int Compare(Person left, Person right) =>
left.FirstName.CompareTo(right.FirstName);
}
public class Behavior
{
public virtual IComparer<Person> GetComparer() => PersonByFirstNameComparer();
}
public class PersonByLastNameComparer : IComparer<Person>
{
public int Compare(Person left, Person right) =>
left.LastName.CompareTo(right.LastName);
}
public class SpecializedBehavior : Behavior
{
public override IComparer<Person> GetComparer() => new PersonByLastNameComparer();
}
可以通过以下方式使用:
public static void Main()
{
var people = new List<Person>
{
new Person{Age=10, FirstName="Pelle", LastName="Larsson"},
new Person{Age=90, FirstName="Nils", LastName="Nilsson"},
new Person{Age=15, FirstName="Olle", LastName="Johansson"},
new Person{Age=30, FirstName="Kalle", LastName="Svensson"}
};
var standardBehavior = new Behavior();
IEnumerable<Person> orderedPeople1 =
people.OrderBy(p => p, standardBehavior.GetComparer());
foreach (Person p in orderedPeople1)
{
Console.WriteLine($"{p.FirstName} {p.LastName}");
}
var specializedBehavior = new SpecializedBehavior();
IEnumerable<Person> orderedPeople2 =
people.OrderBy(p => p, specializedBehavior.GetComparer());
foreach (Person p in orderedPeople2)
{
Console.WriteLine($"{p.FirstName} {p.LastName}");
}
}