Lamba Expression用于调用方法 [英] Lamba Expression to invoke a Method
问题描述
我有列表< data>
我试图循环并将其传递给某个功能。
IEnumerable< EntityObject> Data = entity。 get ();
LstAll = new Employee();
foreach (EntityObject o in Data)
{
Employee e = new Employee();
DeleteData( 0 ,e);
LstAll.Add(e);
}
现在我想使用lamba表达式,这样我就可以删除Foreach循环并调用DeleteData。
LstAll.Add(来自 o 数据
让 c = DeleteData(o,Employee e));
此代码给我错误。
任何建议Plz帮助。
这里的解决方案:
Linq查询 - 呼叫方法。 [ ^ ]
可能会解决您的问题。
您有2个操作要执行,填写列表和删除记录,两者都无法完成。
>
每个Linq表达式必须有一个选择
。
来自 ... ... 选择 ...;
您的示例数据不完整或可能有误。你说的是列表< data>
,但这在你的代码中是不可见的。
你的LstAll
似乎是一个列表(你在循环中调用Add(...)
),但是初始化是由一个元素组成的(new Employee ()
不太可能是一个列表,对吗?)。
请修改你的问题并仔细检查问题中的代码所以它完全符合你在家庭作业中所拥有的。
干杯
Andi
PS:DeleteData(...)
在做什么?如果它从列表中删除了一些项目,那么程序就会被破坏。
PPS:如果问题是要向列表中添加一系列元素,那么使用AddRange(...)
方法。
PPPS:这是一个错字还是你不明白在哪里使用/不使用类型名称(见< b>员工):LstAll.Add 范围 ( 来自 o 数据让 c = DeleteData (o,员工e) ... 选择 ... );还是以下?
LstAll.Add 范围 (来自 o 数据让 c = DeleteData(o, new 员工() ) ... 选择 ... ) ;
Hi,
I have a List<data>
which i am trying to loop and pass it to some function.
IEnumerable<EntityObject> Data = entity.get();
LstAll = new Employee();
foreach (EntityObject o in Data)
{
Employee e = new Employee();
DeleteData(0,e);
LstAll.Add(e);
}
now i want to use lamba expression so that i can remove the Foreach loop and call the DeleteData.
LstAll.Add(from o in Data
let c = DeleteData(o,Employee e));
This code is giving me error.
Any suggestion Plz help.
[Edit: Code block added]
The solution here:
Linq Query - Call Method.[^]
might solve your issue.
You have 2 operations to perform, fill list and delete record, both cannot be done.
Each Linq expression must have aselect
.
from ... in ... select ...;
Your example data is incomplete or maybe wrong. You talk aboutList<data>
but this is nowhere visible in your code.
YourLstAll
seems to be a list (you callAdd(...)
in the loop), but initialization is by a single element (new Employee()
is not likely to be a list, right?).
Please revise your question and carefully check the code in the question so it exactly matches what you have in your homework assignment.
Cheers
Andi
PS: What doesDeleteData(...)
doing? If it removes some item from the list it loops over, the program is broken.
PPS: If the problem is to add a range of elements to a list, then use theAddRange(...)
method.
PPPS: Is this a typo or don''t you understand where to use/don''t use type names (seeEmployee):LstAll.AddRange(from o in Data let c = DeleteData(o,Employeee) ... select ...);or is it rather the following?
LstAll.AddRange(from o in Data let c = DeleteData(o, new Employee()) ... select ...);
这篇关于Lamba Expression用于调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!