Using LINQ on the client-side
WebSharper provides support to process IEnumerable
sequences using LINQ
queries on the client side.
Simple queries
You can use LINQ query syntax in client-side code like you would in .NET.
Of course, you can use explicit LINQ extension methods if you prefer.
Other Linq methods
WebSharper provides proxies for all IEnumerable
extension methods from
System.Linq
, such as .Concat()
, .ToArray()
or .ElementAt()
:
Using custom comparers
Advanced uses of Linq methods are possible, such as custom comparers. Here is
an example where we make a sequence's items distinct by their length using an
EqualityComparer
:
Methods dealing with default values
Some Linq methods use the default value of the element type. For example,
.FirstOrDefault()
returns the first element of the IEnumerable
, or a
default type if it is empty. This default value depends on the type: for
example, if the element type is int
, then the default value is 0
, whereas
if the element type is a class, then the default value is null
. WebSharper
can provide the correct default value, on the condition that the element type
is known statically. For example, the following will run correctly:
However, the following will fail to compile because the element type is not known statically:
In this case, you can add the [Inline]
attribute to the method to make its content to be inserted at every call point, with all code generation needing known types to be resolved appropriately.