如何使用通用列表和 linq 从 Func 获取数据
how to fetch data from Func using generic list and linq
我是 C# 新手。
我有一个 class account
name
和 balance
。
我列出了这些帐户,现在我要做的是以某种方式让所有余额大于 20000
的用户使用 Func
.
我知道我的代码可能有误,所以请帮助我。
class entry
{
public static void Main(string[] args)
{
List<account> list1 = new List<account>()
{
new savingAcc("a",50000),
new currentAcc("b",30000),
new savingAcc("c",80000),
new currentAcc("d",10000),
new savingAcc("e",7000),
new savingAcc("f",85000)
};
Func<List<account>,List<account>> myhand = mySorting.mysal;
/* here something which will print my data as foreach is not working */
}
}
public class mySorting
{
public static List<account> mysal(List<account> lis)
{
return (from i in lis where i._Balance > 50000 select i).ToList();
}
}
你可以做这样的事情,而不用 Func<>
foreach(account in mySorting.mysal(list1)) {
/* do your stuff on an account */
}
或更好
foreach(account in (from i in lis where i._Balance > 50000 select i)) {
/* do your stuff on an account */
}
甚至更好
(from i in lis where i._Balance > 50000 select i).ForEach(a => a.DoStuff());
编辑:
有了 Func<>
你可以使用
Func<List<account>, List<account>> myhand = mySorting.mysal;
foreach(account in myhand(list1)) {
/* do your stuff on an account */
}
或
Func<List<account>, List<account>> myhand = mySorting.mysal;
myhand(list1).ForEach(a => a.DoStuff());
我是 C# 新手。
我有一个 class account
name
和 balance
。
我列出了这些帐户,现在我要做的是以某种方式让所有余额大于 20000
的用户使用 Func
.
我知道我的代码可能有误,所以请帮助我。
class entry
{
public static void Main(string[] args)
{
List<account> list1 = new List<account>()
{
new savingAcc("a",50000),
new currentAcc("b",30000),
new savingAcc("c",80000),
new currentAcc("d",10000),
new savingAcc("e",7000),
new savingAcc("f",85000)
};
Func<List<account>,List<account>> myhand = mySorting.mysal;
/* here something which will print my data as foreach is not working */
}
}
public class mySorting
{
public static List<account> mysal(List<account> lis)
{
return (from i in lis where i._Balance > 50000 select i).ToList();
}
}
你可以做这样的事情,而不用 Func<>
foreach(account in mySorting.mysal(list1)) {
/* do your stuff on an account */
}
或更好
foreach(account in (from i in lis where i._Balance > 50000 select i)) {
/* do your stuff on an account */
}
甚至更好
(from i in lis where i._Balance > 50000 select i).ForEach(a => a.DoStuff());
编辑:
有了 Func<>
你可以使用
Func<List<account>, List<account>> myhand = mySorting.mysal;
foreach(account in myhand(list1)) {
/* do your stuff on an account */
}
或
Func<List<account>, List<account>> myhand = mySorting.mysal;
myhand(list1).ForEach(a => a.DoStuff());