Lamba Expression用于调用方法 [英] Lamba Expression to invoke a Method

查看:110
本文介绍了Lamba Expression用于调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有列表< 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 a select.

from ... in ... select ...;


Your example data is incomplete or maybe wrong. You talk about List<data> but this is nowhere visible in your code.
Your LstAll seems to be a list (you call Add(...) 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 does DeleteData(...) 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 the AddRange(...) method.
PPPS: Is this a typo or don''t you understand where to use/don''t use type names (see Employee):

LstAll.AddRange(from o in Data let c = DeleteData(o,Employee e) ... select ...);

or is it rather the following?

LstAll.AddRange(from o in Data let c = DeleteData(o, new Employee()) ... select ...);


这篇关于Lamba Expression用于调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆