5. Could there be more? Essential LINQThe monadic Bind operator IEnumerable<T> IQueryable<T> new[]{ 42 } IEnumerable<T> SelectMany<T, R>(this IEnumerable<T> source,Func<T, IEnumerable<R>> selector) SelectMany Also see www.codeplex.com/LINQSQO for “Project MinLINQ”
6. Essential LINQMaybe monad (for fun and no profit) Null-propagating dot string s = name?.ToUpper(); One single library function suffices Syntactic sugar name.SelectMany( _ => _.ToUpper(), s => s) from _ in name from s in _.ToUpper() select s Compiler
7. Essential LINQ Bart J.F. De Smet Software Development Engineer Cloud Programmability Team demo
8. Query Providers RevisitedWhy do we have IQueryable<T>? Does it really have to be a runtime check? Implements IQueryable<T> varsrc = newQueryProvider<Product>(); varres = from p insrc wherep.Price > 100m groupp byp.Category; foreach(var p in res) Console.WriteLine(p); Compiles fine
9. Query Providers RevisitedLeveraging the query pattern varres = from p insrc wherep.Price > 100m groupp byp.Category; Syntactic sugar varres = src .Where(p => p.Price> 100m) .GroupBy(p => p.Category); No GroupBy“edge” Can be instance methods Select Where Source<T> Filtered<T> Projected<T>
10. Query Providers RevisitedTaking it one step further Recipe Method overloads Type state machine Operator overloads varres = fromtweet intwitter where tweet. == “Bart” From About From Location From == “LINQ” where tweet. About About About Location select tweet; Query “learns” From operator overloading “Has a” type classTwitter { publicTwitterByFromWhere(Func<TweetAboutFromLoc, FilterFrom> filter); // Other filter methods } classTwitterByFrom { publicTwitterByAboutFromWhere(Func<TweetAboutLoc, FilterAbout> filter); // Other filter methods // Fields with current filters } Custom syntax trees
11. Query Providers Revisited Bart J.F. De Smet Software Development Engineer Cloud Programmability Team demo
16. Asynchronous Data AccessEssential interfaces interfaceIObservable<outT> { IDisposableSubscribe(IObserver<T> observer);} interfaceIObserver<in T> { voidOnNext(T value); voidOnError(Exception ex); voidOnCompleted(); } Both interfaces ship in the .NET 4 BCL
17. IQbservable<T> LINQ to WMI Events How? ToQbservable Translatable (Expression trees) IQueryable<T> LINQ to SQL ToQueryable LINQ to *.* AsObservable Homo-iconic AsEnumerable AsQbservable AsQueryable ToObservable IEnumerable<T> LINQ to Objects IObservable<T> LINQ to Events Fixed (MSIL) ToEnumerable Pull(interactive) Push (reactive) What? Duality Concurrency(IScheduler) Where? Message loops Distributed Worker pools Threads
18. IObservable<T> Asynchronous Data AccessIQbservable<T> interfaceIQbservable<outT> : IObservable<T> { ExpressionExpression { get; } TypeElementType { get; } IQbservableProvider Provider { get; } } interfaceIQbservableProvider { IQbservable<R> CreateQuery<R>(Expression expression); } We welcome semantic discussions. Extended role for some operators No Execute method
19. LINQ to WMI Events (WQL) Bart J.F. De Smet Software Development Engineer Cloud Programmability Team demo
20. Asynchronous Data AccessC# 5.0 and VB 11.0 “await” One versus many? IObservable<T> interface – many results Task<T> concrete class – one result Task<T> based language feature Rx bridge by implementing the “await pattern” IAsyncEnumerable<T>
21. Asynchronous Bridge for C# and VB “await” Bart J.F. De Smet Software Development Engineer Cloud Programmability Team announcing
23. Where execution happensIScheduler specifies “where” Imported TextBoxTextChangedevent var res = from word ininput.DistinctUntilChanged() .Throttle(TimeSpan.FromSeconds(0.5)) from words in lookup(word) select words; Asynchronous web service call Indicates where things run Use of scheduler to synchronize res.Subscribe(words => { lst.Items.Clear(); lst.Items.AddRange((fromwordinwords selectword.Word).ToArray());}); res.ObserveOn(newControlScheduler(frm)).Subscribe(words => { lst.Items.Clear(); lst.Items.AddRange((fromwordinwords selectword.Word).ToArray());});
24. Where execution happensIScheduler parameterization Baked in notion of “where”? Entry-point for the schema Custom schedulers could be very rich (e.g. server farm) varctx = new NorthwindDataContext(); var res = from product inctx.Products whereproduct.Price > 100m selectproduct.Name; Decoupled “what” from “where” foreach(varproduct inres.RemoteOn(newSqlScheduler(“server”))) // Process product
25. Where execution happensExpression tree remoting Rx .NET fromtickerinstocks whereticker.Symbol == “MSFT” selectticker.Quote Observable data source JSON serializer Retargeting to AJAX RxJS stocks .Where(function (t) { returnt.Symbol == “MSFT”; }) .Select(function (t) { returnt.Quote; })
26. Remoting of Query Operators Bart J.F. De Smet Software Development Engineer Cloud Programmability Team demo