DotnetSparrow
DotnetSparrow

Reputation: 27996

lambda expression meaning

I have created an htmlhelper which has signature like this:

 public static MvcHtmlString PageLinks(this HtmlHelper html,
     PagingInfo pagingInfo,Func<int, string> pageUrl)

and I see that It is passed like this:

@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new {page = x}))

but I dont know what is it meaning ?

x => Url.Action("List", new {page = x})

Please suggest meaning of this lambda expression

Upvotes: 0

Views: 719

Answers (2)

xanatos
xanatos

Reputation: 111810

x => Url.Action("List", new {page = x})

This is a (lambda) function that given an x (and we know that x must be integer because the signature is Func<int, string>) returns an url (the string). (technically it is a lambda expression, but the difference is lost in this particular case)

You use it like this: string url = pageUrl(pageNumber);

Url.Action gives the url of the action List that has a single parameter (of type int and name page probably) that will be filled with the value of x. This parameter will probably be the page of the list that must be returned.

@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new {page = x}))

This will "return" an html string with a link to the url we calculated before. (probably an <a href='url'></a> or something similar) ah... it's your function :-)

(I think you are doing something like this: ASP.NET MVC Paging, Preserving Current Query Parameters with the PagingInfo class containing the current and total pages, right?)

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

Well, it's a lambda expression which takes a single parameter called x and is implicitly typed to be a int, and which calls Url.Action with one argument of the string "List" another argument of an anonymous type which has a single property page with the value of the parameter x.

The value returned from Url.Action is used as the return value for the lambda expression.

That's how the lambda expression itself is broken down, in terms of language constructs. If you're asking what it means in terms of behaviour, that's a different matter - you'd need to look at your own PageLinks method, as well as the documentation for Url.Action.

Upvotes: 1

Related Questions