LINQ, Take TwoRealizing the LINQ to Everything DreamBart J.F. De SmetSoftware Development EngineerMicrosoft Corporation
What’s LINQ?A historical perspective5 years agoLittle recent innovationWhere’s the cloud?Censored
Essential LINQLanguage Integrated MonadsWhy?What?How?
Could there be more?Essential LINQThe monadic Bind operatorIEnumerable<T>IQueryable<T>new[]{ 42 }IEnumerable<T> SelectMany<T, R>(this IEnumerable<T> source,Func<T, IEnumerable<R>> selector)SelectManyAlso see www.codeplex.com/LINQSQO for “Project MinLINQ”
Essential LINQMaybe monad (for fun and no profit)Null-propagating dotstring s = name?.ToUpper();One single library function sufficesSyntactic          sugarname.SelectMany(    _ => _.ToUpper(),    s => s)from _ in namefrom s in _.ToUpper()select sCompiler
Essential LINQBart J.F. De SmetSoftware Development EngineerCloud Programmability Teamdemo
Query Providers RevisitedWhy do we have IQueryable<T>?Does it really have to be a runtime check?ImplementsIQueryable<T>varsrc = newQueryProvider<Product>();varres = from p insrcwherep.Price > 100mgroupp byp.Category;foreach(var p in res)Console.WriteLine(p);Compiles fine
Query Providers RevisitedLeveraging the query patternvarres = from p insrc          wherep.Price > 100mgroupp byp.Category;Syntactic sugarvarres = src.Where(p => p.Price> 100m).GroupBy(p => p.Category);No GroupBy“edge”Can be instance methodsSelectWhereSource<T>Filtered<T>Projected<T>
Query Providers RevisitedTaking it one step furtherRecipeMethod overloadsType state machineOperator overloadsvarres = fromtweet intwitter          where tweet.    == “Bart”FromAboutFromLocationFrom     == “LINQ”          where tweet.AboutAboutAboutLocation          select tweet;Query “learns”From operator overloading“Has a” typeclassTwitter{publicTwitterByFromWhere(Func<TweetAboutFromLoc, FilterFrom> filter);// Other filter methods}classTwitterByFrom{publicTwitterByAboutFromWhere(Func<TweetAboutLoc, FilterAbout> filter);// Other filter methods    // Fields with current filters}Custom syntax trees
Query Providers RevisitedBart J.F. De SmetSoftware Development EngineerCloud Programmability Teamdemo
Asynchronous Data Access(𝑓 ◦ 𝑔)(𝑥) = 𝑓(𝑔(𝑥)) Way simpler with RxRx is a library for composing asynchronous and event-basedprograms using observable collections.Queries! LINQ!Download at MSDN DevLabs.NET 3.5 SP1 and 4.0, Silverlight 3 and 4
JavaScript (RxJS)
XNA 3.1 for XBOX and Zune
Windows Phone 7Asynchronous Data AccessPush-based data retrievalApplicationGot next?MoveNextInteractiveReactiveOnNextHave next!EnvironmentIObservable<T>IObserver<T>IEnumerable<T>IEnumerator<T>
Asynchronous Data AccessEssential interfacesinterfaceIObservable<outT>{IDisposableSubscribe(IObserver<T> observer);}interfaceIObserver<in T>{voidOnNext(T value);voidOnError(Exception ex);voidOnCompleted();}Both interfaces ship in the .NET 4 BCL
IQbservable<T>LINQ to WMI EventsHow?ToQbservableTranslatable(Expression trees)IQueryable<T>LINQ to SQLToQueryableLINQ to *.*AsObservableHomo-iconicAsEnumerableAsQbservableAsQueryableToObservableIEnumerable<T>LINQ to ObjectsIObservable<T>LINQ to EventsFixed(MSIL)ToEnumerablePull(interactive)Push(reactive)What?DualityConcurrency(IScheduler)Where?Message loopsDistributedWorker poolsThreads
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 operatorsNo Execute method
LINQ to WMI Events (WQL)Bart J.F. De SmetSoftware Development EngineerCloud Programmability Teamdemo
Asynchronous Data AccessC# 5.0 and VB 11.0 “await”One versus many?IObservable<T> interface – many resultsTask<T> concrete class – one resultTask<T> based language featureRx bridge by implementing the “await pattern”IAsyncEnumerable<T>
Asynchronous Bridge for C# and VB “await”Bart J.F. De SmetSoftware Development EngineerCloud Programmability Teamannouncing
Where execution happensIScheduler interfaceSystem.Concurrencyin System.CoreExIntroduction of concurrencyinterfaceIScheduler{DateTimeOffsetNow { get; }IDisposable Schedule(Action action);IDisposable Schedule(Actionaction, TimeSpandueTime);}ToObservableIObservable<T>IEnumerable<T>ToEnumerableSynchronousAsynchronous
Where execution happensIScheduler specifies “where”Imported TextBoxTextChangedeventvar res = from word ininput.DistinctUntilChanged()                            .Throttle(TimeSpan.FromSeconds(0.5))from words in lookup(word)select words;Asynchronous web service callIndicates where things runUse of scheduler to synchronizeres.Subscribe(words => {lst.Items.Clear();lst.Items.AddRange((fromwordinwordsselectword.Word).ToArray());});res.ObserveOn(newControlScheduler(frm)).Subscribe(words => {lst.Items.Clear();lst.Items.AddRange((fromwordinwordsselectword.Word).ToArray());});
Where execution happensIScheduler parameterizationBaked in notion of “where”?Entry-point for the schemaCustom schedulers could be very rich (e.g. server farm)varctx = new NorthwindDataContext(); var res = from product inctx.Productswhereproduct.Price > 100mselectproduct.Name;Decoupled “what” from “where”foreach(varproduct inres.RemoteOn(newSqlScheduler(“server”)))    // Process product
Where execution happensExpression tree remotingRx .NETfromtickerinstockswhereticker.Symbol == “MSFT”selectticker.QuoteObservable data sourceJSON              serializerRetargeting to AJAXRxJSstocks.Where(function (t) { returnt.Symbol == “MSFT”; }).Select(function (t) { returnt.Quote; })
Remoting of Query OperatorsBart J.F. De SmetSoftware Development EngineerCloud Programmability Teamdemo
LINQ to the unexpectedConstraint solvingNon-persistentModel[Decisions[Reals, SA, VZ],  Goals[Minimize[20 * SA + 15 * VZ]  ],Constraints[     C1 -> 0.3 * SA + 0.4 * VZ >= 2000,    C2 -> 0.4 * SA + 0.2 * VZ >= 1500,    C3 -> 0.2 * SA + 0.3 * VZ >= 500,    C4 -> SA <= 9000,    C5 -> VZ <= 6000,    C6 -> SA >= 0,    C7 -> VZ >= 0  ]]fromminctx.CreateModel(new {vz = default(double),   sa = default(double)})where 0.3 * m.sa + 0.4 * m.vz >= 2000   && 0.4 * m.sa + 0.2 * m.vz >= 1500   && 0.2 * m.sa + 0.3 * m.vz >= 500where 0 <= m.sa && m.sa <= 9000   && 0 <= m.vz && m.vz <= 6000orderby 20 * m.sa + 15 * m.vzselectmCost / barrelRefinement %Max # barrelsMin # barrelsTo compute call Solve
LINQ to the unexpectedJoining with reactive sourcesObservabledata sourcesfromcostSAinGetPriceMonitor("SA")fromcostVZinGetPriceMonitor("VZ")fromminctx.CreateModel(new { vz = default(double),sa = default(double) })where0.3 * m.sa + 0.4 * m.vz >= 2000   && 0.4 * m.sa + 0.2 * m.vz >= 1500   && 0.2 * m.sa + 0.3 * m.vz >= 500where0 <= m.sa && m.sa <= 9000   && 0 <= m.vz && m.vz <= 6000orderbycostSA* m.sa + costVZ* m.vzselectmSelectManySubscribe here!Parametersto decision
Theorem Solving using Z3Bart J.F. De SmetSoftware Development EngineerCloud Programmability Teamdemo

Ft10 de smet

  • 2.
    LINQ, Take TwoRealizingthe LINQ to Everything DreamBart J.F. De SmetSoftware Development EngineerMicrosoft Corporation
  • 3.
    What’s LINQ?A historicalperspective5 years agoLittle recent innovationWhere’s the cloud?Censored
  • 4.
  • 5.
    Could there bemore?Essential LINQThe monadic Bind operatorIEnumerable<T>IQueryable<T>new[]{ 42 }IEnumerable<T> SelectMany<T, R>(this IEnumerable<T> source,Func<T, IEnumerable<R>> selector)SelectManyAlso see www.codeplex.com/LINQSQO for “Project MinLINQ”
  • 6.
    Essential LINQMaybe monad(for fun and no profit)Null-propagating dotstring s = name?.ToUpper();One single library function sufficesSyntactic sugarname.SelectMany( _ => _.ToUpper(), s => s)from _ in namefrom s in _.ToUpper()select sCompiler
  • 7.
    Essential LINQBart J.F.De SmetSoftware Development EngineerCloud Programmability Teamdemo
  • 8.
    Query Providers RevisitedWhydo we have IQueryable<T>?Does it really have to be a runtime check?ImplementsIQueryable<T>varsrc = newQueryProvider<Product>();varres = from p insrcwherep.Price > 100mgroupp byp.Category;foreach(var p in res)Console.WriteLine(p);Compiles fine
  • 9.
    Query Providers RevisitedLeveragingthe query patternvarres = from p insrc wherep.Price > 100mgroupp byp.Category;Syntactic sugarvarres = src.Where(p => p.Price> 100m).GroupBy(p => p.Category);No GroupBy“edge”Can be instance methodsSelectWhereSource<T>Filtered<T>Projected<T>
  • 10.
    Query Providers RevisitedTakingit one step furtherRecipeMethod overloadsType state machineOperator overloadsvarres = fromtweet intwitter where tweet. == “Bart”FromAboutFromLocationFrom == “LINQ” where tweet.AboutAboutAboutLocation select tweet;Query “learns”From operator overloading“Has a” typeclassTwitter{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 RevisitedBartJ.F. De SmetSoftware Development EngineerCloud Programmability Teamdemo
  • 12.
    Asynchronous Data Access(𝑓 ◦ 𝑔)(𝑥) = 𝑓(𝑔(𝑥)) Waysimpler with RxRx is a library for composing asynchronous and event-basedprograms using observable collections.Queries! LINQ!Download at MSDN DevLabs.NET 3.5 SP1 and 4.0, Silverlight 3 and 4
  • 13.
  • 14.
    XNA 3.1 forXBOX and Zune
  • 15.
    Windows Phone 7AsynchronousData AccessPush-based data retrievalApplicationGot next?MoveNextInteractiveReactiveOnNextHave next!EnvironmentIObservable<T>IObserver<T>IEnumerable<T>IEnumerator<T>
  • 16.
    Asynchronous Data AccessEssentialinterfacesinterfaceIObservable<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 WMIEventsHow?ToQbservableTranslatable(Expression trees)IQueryable<T>LINQ to SQLToQueryableLINQ to *.*AsObservableHomo-iconicAsEnumerableAsQbservableAsQueryableToObservableIEnumerable<T>LINQ to ObjectsIObservable<T>LINQ to EventsFixed(MSIL)ToEnumerablePull(interactive)Push(reactive)What?DualityConcurrency(IScheduler)Where?Message loopsDistributedWorker poolsThreads
  • 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 operatorsNo Execute method
  • 19.
    LINQ to WMIEvents (WQL)Bart J.F. De SmetSoftware Development EngineerCloud Programmability Teamdemo
  • 20.
    Asynchronous Data AccessC#5.0 and VB 11.0 “await”One versus many?IObservable<T> interface – many resultsTask<T> concrete class – one resultTask<T> based language featureRx bridge by implementing the “await pattern”IAsyncEnumerable<T>
  • 21.
    Asynchronous Bridge forC# and VB “await”Bart J.F. De SmetSoftware Development EngineerCloud Programmability Teamannouncing
  • 22.
    Where execution happensISchedulerinterfaceSystem.Concurrencyin System.CoreExIntroduction of concurrencyinterfaceIScheduler{DateTimeOffsetNow { get; }IDisposable Schedule(Action action);IDisposable Schedule(Actionaction, TimeSpandueTime);}ToObservableIObservable<T>IEnumerable<T>ToEnumerableSynchronousAsynchronous
  • 23.
    Where execution happensISchedulerspecifies “where”Imported TextBoxTextChangedeventvar res = from word ininput.DistinctUntilChanged() .Throttle(TimeSpan.FromSeconds(0.5))from words in lookup(word)select words;Asynchronous web service callIndicates where things runUse of scheduler to synchronizeres.Subscribe(words => {lst.Items.Clear();lst.Items.AddRange((fromwordinwordsselectword.Word).ToArray());});res.ObserveOn(newControlScheduler(frm)).Subscribe(words => {lst.Items.Clear();lst.Items.AddRange((fromwordinwordsselectword.Word).ToArray());});
  • 24.
    Where execution happensISchedulerparameterizationBaked in notion of “where”?Entry-point for the schemaCustom schedulers could be very rich (e.g. server farm)varctx = new NorthwindDataContext(); var res = from product inctx.Productswhereproduct.Price > 100mselectproduct.Name;Decoupled “what” from “where”foreach(varproduct inres.RemoteOn(newSqlScheduler(“server”))) // Process product
  • 25.
    Where execution happensExpressiontree remotingRx .NETfromtickerinstockswhereticker.Symbol == “MSFT”selectticker.QuoteObservable data sourceJSON serializerRetargeting to AJAXRxJSstocks.Where(function (t) { returnt.Symbol == “MSFT”; }).Select(function (t) { returnt.Quote; })
  • 26.
    Remoting of QueryOperatorsBart J.F. De SmetSoftware Development EngineerCloud Programmability Teamdemo
  • 27.
    LINQ to theunexpectedConstraint solvingNon-persistentModel[Decisions[Reals, SA, VZ], Goals[Minimize[20 * SA + 15 * VZ] ],Constraints[ C1 -> 0.3 * SA + 0.4 * VZ >= 2000, C2 -> 0.4 * SA + 0.2 * VZ >= 1500, C3 -> 0.2 * SA + 0.3 * VZ >= 500, C4 -> SA <= 9000, C5 -> VZ <= 6000, C6 -> SA >= 0, C7 -> VZ >= 0 ]]fromminctx.CreateModel(new {vz = default(double), sa = default(double)})where 0.3 * m.sa + 0.4 * m.vz >= 2000 && 0.4 * m.sa + 0.2 * m.vz >= 1500 && 0.2 * m.sa + 0.3 * m.vz >= 500where 0 <= m.sa && m.sa <= 9000 && 0 <= m.vz && m.vz <= 6000orderby 20 * m.sa + 15 * m.vzselectmCost / barrelRefinement %Max # barrelsMin # barrelsTo compute call Solve
  • 28.
    LINQ to theunexpectedJoining with reactive sourcesObservabledata sourcesfromcostSAinGetPriceMonitor("SA")fromcostVZinGetPriceMonitor("VZ")fromminctx.CreateModel(new { vz = default(double),sa = default(double) })where0.3 * m.sa + 0.4 * m.vz >= 2000 && 0.4 * m.sa + 0.2 * m.vz >= 1500 && 0.2 * m.sa + 0.3 * m.vz >= 500where0 <= m.sa && m.sa <= 9000 && 0 <= m.vz && m.vz <= 6000orderbycostSA* m.sa + costVZ* m.vzselectmSelectManySubscribe here!Parametersto decision
  • 29.
    Theorem Solving usingZ3Bart J.F. De SmetSoftware Development EngineerCloud Programmability Teamdemo