LINQ Take Two Realizing the LINQ to Everything

  • Slides: 31
Download presentation
LINQ, Take Two Realizing the LINQ to Everything Dream Bart J. F. De Smet

LINQ, Take Two Realizing the LINQ to Everything Dream Bart J. F. De Smet blogs. bartdesmet. net/bartde@microsoft. com

A Historical Perspective 5 years ago Little recent innovation Censored Where’s the cloud?

A Historical Perspective 5 years ago Little recent innovation Censored Where’s the cloud?

Monads Language Integrated Query Why? How? What?

Monads Language Integrated Query Why? How? What?

The Monadic Bind Operator Revealed IEnumerable<T> IQueryable<T> Could there be more? new[] { 42

The Monadic Bind Operator Revealed IEnumerable<T> IQueryable<T> Could there be more? new[] { 42 } Select. Many IEnumerable<R> Select. Many<T, R>( this IEnumerable<T> source, Func<T, IEnumerable<R>> selector) Also see www. codeplex. com/LINQSQO for “Project Min. LINQ”

Building the Maybe Monad (For Fun and No Profit) Null-propagating dot One single library

Building the Maybe Monad (For Fun and No Profit) Null-propagating dot One single library function suffices string s = name? . To. Upper(); Syntactic sugar from _ in name from s in _. To. Upper() select s Compiler name. Select. Many( _ => _. To. Upper(), s => s)

DEMO Essential LINQ

DEMO Essential LINQ

Query Providers Revisited – Do We Need IQueryable<T>? Implements IQueryable<T> var src = new

Query Providers Revisited – Do We Need IQueryable<T>? Implements IQueryable<T> var src = new Source<Product>(); var res = from p in src where p. Price > 100 m group p by p. Category; Compiles fine foreach (var p in res) Console. Write. Line(p); Does it really have to be a runtime check?

Leveraging The Query Pattern var res = from p in src where p. Price

Leveraging The Query Pattern var res = from p in src where p. Price > 100 m Syntactic sugar group p by p. Category; var res = src. Where(p => p. Price > 100 m). Group. By(p => p. Category); No Group. By “edge” Can be instance methods Source<T> Where Filtered<T> Select Projected<T>

Taking It One Step Further var res = from tweet in twitter Query “learns”

Taking It One Step Further var res = from tweet in twitter Query “learns” class Tweet. About. From. Loc class Tweet. About. Loc { { where tweet. From == “Bart” public From. String From; public About. String About; About From == “LINQ” public Loc. String Location; where tweet. About public Loc. String Location; From } } select tweet; About Location “Has a” type class About. String class From. String Recipe { { Filter. About operator ==( Filter. From operator ==( Method overloads About. String f, string s) From. String f, machine string s) Type state } From operator overloading } Operator overloads class Twitter. By. From { public Twitter. By. From Twitter. By. About. From Where(Func<Tweet. About. From. Loc, Where(Func<Tweet. About. Loc, Filter. From> Filter. About> filter); // Other filter methods } // Fields with current filters } Custom syntax trees

DEMO Query Providers Revisited

DEMO Query Providers Revisited

Event Processing Systems Way simpler with Rx Rx is a library for composing asynchronous

Event Processing Systems Way simpler with Rx Rx is a library for composing asynchronous and event-based programs using observable sequences. Queries! LINQ! • • • . NET 3. 5 SP 1 and 4. 0 Silverlight 3, 4, and 5 XNA 4. 0 for Xbox 360 Windows Phone 7 Java. Script (Rx. JS) Download at MSDN Data Developer Center or use Nu. Get

Push-Based Data Retrieval Move. Next IEnumerable<T> IEnumerator<T> Environment Have next! IObservable<T> IObserver<T> Reactive Got

Push-Based Data Retrieval Move. Next IEnumerable<T> IEnumerator<T> Environment Have next! IObservable<T> IObserver<T> Reactive Got next? On. Next Interactive Application

Event Programming Interfaces Both interfaces ship in the. NET 4 BCL interface IObservable<out T>

Event Programming Interfaces Both interfaces ship in the. NET 4 BCL interface IObservable<out T> { IDisposable Subscribe(IObserver<T> observer); } interface IObserver<in T> { void On. Next(T value); void On. Error(Exception ex); void On. Completed(); }

How? As. Queryable (MSIL) To. Observable Pull e? rre ler) r he ncu edu

How? As. Queryable (MSIL) To. Observable Pull e? rre ler) r he ncu edu (interactive) Threads Worker pools Duality As. Qbservable (Expression trees) Translatable Homo-iconic Fixed LINQ to *. * As. Observable LINQ to Objects y nc W Co Sch (I As. Enumerable IEnumerable<T> LINQ to WMI Events To. Enumerable LINQ to SQL IQbservable<T> To. Queryable IQueryable<T> To. Qbservable IObservable<T> LINQ to Events Push What? (reactive) Message loops Distributed

IQbservable<T> – The Dual of IQueryable<T> interface IQbservable<out T> : IObservable<T> { Expression {

IQbservable<T> – The Dual of IQueryable<T> interface IQbservable<out T> : IObservable<T> { Expression { get; } Type Element. Type { get; } IQbservable. Provider { get; } } We welcome semantic discussions Extended role for some operators No Execute interface IQbservable. Provider method { IQbservable<R> Create. Query<R>(Expression expression); }

DEMO LINQ to WMI Events (WQL)

DEMO LINQ to WMI Events (WQL)

Rx and the Visual Studio Async CTP • The essence of the feature Result

Rx and the Visual Studio Async CTP • The essence of the feature Result is async too Asynchronously computed value async Task<int> Get. Length(Task<string> name) { string n = await name; Can await a return n. Length; “future” value } • Feature based on the “await-pattern” • Awaiting Task<T> • Returns the single result (of type T) • Awaiting IObservable<T>? • We return the last result (of type T) • Want all values? Use To. List() first!

Asynchronous Pull-Based Data Access? • Await feature is about sequential code • Great for

Asynchronous Pull-Based Data Access? • Await feature is about sequential code • Great for single-valued computations • What about asynchronous pull-based data collections? public interface IAsync. Enumerable<T> { IAsync. Enumerator<T> Get. Enumerator(); } Can await Rx defines Query Operators public interface IAsync. Enumerator<T> : IDisposable { T Current { get; } Task<bool> Move. Next(); }

DEMO Rx support for C# and VB “await”

DEMO Rx support for C# and VB “await”

Where Execution Happens – Introducing Schedulers Rx’s unified way to introduce concurrency interface IScheduler

Where Execution Happens – Introducing Schedulers Rx’s unified way to introduce concurrency interface IScheduler { Date. Time. Offset Now { get; } IDisposable Schedule<T>(T state, Func<IScheduler, T, IDisposable> action); IDisposable Schedule<T>(T state, Time. Span due. Time, Func<IScheduler, T, IDisposable> action); IDisposable Schedule<T>(T state, Date. Time. Offset due. Time, Func<IScheduler, T, IDisposable> action); } To. Observable IObservable<T> IEnumerable<T> To. Enumerable Synchronous Asynchronous

IScheduler Specifies Where Things Happen Imported Text. Box Text. Changed event var res =

IScheduler Specifies Where Things Happen Imported Text. Box Text. Changed event var res = from word in input. Distinct. Until. Changed(). Throttle(Time. Span. From. Seconds(0. 5)) from words in lookup(word) Asynchronous select words; web service call Indicates where things run Use of scheduler to synchronize res. Subscribe(words => res. Observe. On(new Control. Scheduler(frm)). Subscribe(words => { lst. Items. Clear(); lst. Items. Add. Range((from word in words select word. Word). To. Array()); });

IScheduler Parameterization Baked in notion of “where”? Entry-point for the schema var ctx =

IScheduler Parameterization Baked in notion of “where”? Entry-point for the schema var ctx = new Northwind. Data. Context(); var res = from product in ctx. Products where product. Price > 100 m select product. Name; Custom schedulers could be very rich (e. g. server farm) Decoupled “what” from “where” foreach (var p in res. Remote. On(new Sql. Scheduler(“server”))) // Process product

Expression Tree Remoting Rx. NET from ticker in stocks where ticker. Symbol == “MSFT”

Expression Tree Remoting Rx. NET from ticker in stocks where ticker. Symbol == “MSFT” select ticker. Quote Retargeting to AJAX Rx. JS JSON Observable data source serializer stocks. Where(function (t) { return t. Symbol == “MSFT”; }). Select(function (t) { return t. Quote; })

DEMO Remoting of Query Expressions

DEMO Remoting of Query Expressions

LINQ to the Unexpected Refinement % Model[ Decisions[Reals, SA, VZ], Goals[ Minimize[20 * SA

LINQ to the Unexpected Refinement % Model[ Decisions[Reals, SA, VZ], Goals[ Minimize[20 * SA + 15 * VZ] ], Cost / barrel Constraints[ C 1 -> 0. 3 * SA + 0. 4 * VZ >= 2000, C 2 -> 0. 4 * SA + 0. 2 * VZ >= 1500, C 3 -> 0. 2 * SA + 0. 3 * VZ >= 500, C 4 -> SA <= 9000, Max C 5 -> VZ <= 6000, # barrels C 6 -> SA >= 0, Min C 7 -> VZ >= 0 # barrels ] ] Non-persistent from m in ctx. Create. Model(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 >= 500 where 0 <= m. sa && m. sa <= 9000 && 0 <= m. vz && m. vz <= 6000 orderby 20 * m. sa + 15 * m. vz select m To compute call Solve

Select. Many LINQ to the Unexpected Subscribe here! from cost. SA in Get. Price.

Select. Many LINQ to the Unexpected Subscribe here! from cost. SA in Get. Price. Monitor("SA") from cost. VZ in Get. Price. Monitor("VZ") Observable data sources from m in ctx. Create. Model( 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 >= 500 where 0 <= m. sa && m. sa <= 9000 && 0 <= m. vz && m. vz <= 6000 orderby cost. SA * m. sa + cost. VZ * m. vz select m Parameters to decision

DEMO Theorem Solving using Z 3

DEMO Theorem Solving using Z 3

A Futuristic Perspective Next 5 years Remoting Async Rx and Ix are here Solvers

A Futuristic Perspective Next 5 years Remoting Async Rx and Ix are here Solvers Toolkits Scheduler Censored

Stay up to date with MSDN Belux • Register for our newsletters and stay

Stay up to date with MSDN Belux • Register for our newsletters and stay up to date: http: //www. msdn-newsletters. be • Technical updates • Event announcements and registration • Top downloads • Follow our blog Download http: //blogs. msdn. com/belux MSDN/Tech. Net Desktop Gadget http: //bit. ly/msdntngadget • Join us on Facebook http: //www. facebook. com/msdnbelux • Linked. In: http: //linkd. in/msdnbelux/ • Twitter: @msdnbelux

Tech. Days 2011 On-Demand • Watch this session on-demand via Channel 9 http: //channel

Tech. Days 2011 On-Demand • Watch this session on-demand via Channel 9 http: //channel 9. msdn. com/belux • Download to your favorite MP 3 or video player • Get access to slides and recommended resources by the speakers

THANK YOU

THANK YOU