【发布时间】:2016-01-02 12:59:25
【问题描述】:
我有一个包含 Persons 的通用 List<Person> PersonList
class Person
{
public string name {get ; set;}
public string lastName {get ; set;}
public decimal income {get ; set;}
}
我有一个返回收入的方法。
decimal GetPersonIncome(string name)
我有包含我要过滤的名称的枚举:
enum SortednamesA
{
george,
jack,
etc
}
enum SortednamesB
{
bill,
john,
etc
}
我想根据每个枚举的名称创建SortedPersonsIncome 实例。
Class SortedPersonsIncome
{
public SortedPerdons(string customListname,
decimal sortedPersonIncomeA,
decimal sortedPersonIncomeB)
}
所以它会是这样的:
SortedPersonsIncome sortedA = new SortedPersonsIncome(
"newSortedPerson",
GetPersonIncome(georgeFromEnumA),
GetPersonIncome(jackFromEnumA))
我想使用 linq 或 foreach 循环以最少的努力做到这一点。 做这个的最好方式是什么?
谢谢!
【问题讨论】:
-
我用嵌套的 if 尝试了 foreach。
-
我也尝试了来自 linq 的 foreach 和 where 但我是 linq 新手,我找不到解决方案
-
将其添加到问题中
-
为什么不只是
SortedPersonsIncome sortedA = new SortedPersonsIncome( "newSortedPerson", GetPersonIncome(SortednamesA.george.ToString()), GetPersonIncome(SortednamesA.jack.ToString())) -
我使用 Enum 是因为我使用特定的名称,并且我不想输入任何其他名称。
标签: c# .net linq enums generic-list