开发者

C# Lambda Expression Help

If I use a lambda expression like the following

// assume sch_id is a property of the entity Schedules
public void GetRecord(int id)
{
    _myentity.Schedules.Where(x => x.sch_id == id));
}

I'm assuming (although non-tested) I can rewrite that using an anonymous inline function using somet开发者_如何学运维hing like

_jve.Schedules.Where(delegate(Models.Schedules x) { return x.sch_id == id; });

My question is, how would I rewrite that in a normal (non-inline) function and still pass in the id parameter.


The short answer is that you can't make it a stand-along function. In your example, id is actually preserved in a closure.

The long answer, is that you can write a class that captures state by initializing it with the id value you want to operate against, storing it as a member variable. Internally, closures operate similarly - the difference being that they actually capture a reference to the variable not a copy of it. That means that closures can "see" changes to variables they are bound to. See the link above for more details on this.

So, for example:

public class IdSearcher
{
     private int m_Id; // captures the state...
     public IdSearcher( int id ) { m_Id = id; }
     public bool TestForId( in otherID ) { return m_Id == otherID; }
}

// other code somewhere...
public void GetRecord(int id)
{
    var srchr = new IdSearcher( id );
    _myentity.Schedules.Where( srchr.TestForId );
}


If you only want to place the body of the delegate somewhere else you can achieve using this

public void GetRecord(int id)
{
    _myentity.Schedules.Where(x => MyMethodTooLongToPutInline(x, id));
}
private bool MyMethodTooLongToPutInline(Models.Schedules x, int id)
{
    //...
}


You would need to save the ID somewhere. This is done for you by using a closure, which basically is like creating a separate, temporary class with the value and the method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜