开发者

LINQ: Assign Event handler in Select creation

开发者_如何学PythonI have a LINQ statement which creates a number of items

From x in datasource
   select (Customer) new BusinessCustomer(x.SomeThing)

I need to set the BusinessCustomer.OnTapped event handler.

Can this be done in the LINQ statement?


If you can modify the BusinessCustomer class, you could add a constructor that accepts a handler delegate for the OnTapped event.

If you can't or don't want to modify BusinessCustomer to do this, you could use a helper method, which would also allow you to eliminate the cast.

private Customer CreateBusinessCustomer(Thing thing, EventHandler tapHandler)
{
    var customer = new BusinessCustomer(thing);
    customer.OnTapped = tapHandler;
    return customer;
}

private void Customer_OnTapped(object sender, EventArgs e)
{
    // do something
}

Now your query looks like:

from x in datasource
select CreateBusinessCustomer(x.SomeThing, Customer_OnTapped)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜