Presentation is loading. Please wait.

Presentation is loading. Please wait.

LINQ, Take Two Realizing the LINQ to Everything Dream Bart J.F. De Smet blogs.bartdesmet.net/bart

Similar presentations


Presentation on theme: "LINQ, Take Two Realizing the LINQ to Everything Dream Bart J.F. De Smet blogs.bartdesmet.net/bart"— Presentation transcript:

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

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

3 Why? What? How? Language Integrated Query Monads

4 IEnumerable IQueryable IEnumerable IQueryable new[] { 42 } SelectMany IEnumerable SelectMany ( this IEnumerable source, Func > selector) Could there be more? Also see www.codeplex.com/LINQSQO for “Project MinLINQ” The Monadic Bind Operator Revealed

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

6 DEMO Essential LINQ

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

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

9 var res = from tweet in twitter where tweet. About From Location == “Bart” From From About Location == “LINQ” About where tweet. About Query “learns” class Twitter { public TwitterByFrom Where(Func filter); // Other filter methods } Recipe Method overloads Type state machine Operator overloads Recipe Method overloads Type state machine Operator overloads “Has a” type From operator overloading class TwitterByFrom { public TwitterByAboutFrom Where(Func filter); // Other filter methods // Fields with current filters } select tweet; Custom syntax trees class TweetAboutLoc { public AboutString About; public LocString Location; } class AboutString { FilterAbout operator ==( AboutString f, string s) } class TweetAboutLoc { public AboutString About; public LocString Location; } class AboutString { FilterAbout operator ==( AboutString f, string s) } class TweetAboutFromLoc { public FromString From; public AboutString About; public LocString Location; } class FromString { FilterFrom operator ==( FromString f, string s) } class TweetAboutFromLoc { public FromString From; public AboutString About; public LocString Location; } class FromString { FilterFrom operator ==( FromString f, string s) } Taking It One Step Further

10 DEMO Query Providers Revisited

11 Event Processing Systems Rx is a library for composing asynchronous and event-based programs using observable sequences. Queries! LINQ! Way simpler with Rx.NET 3.5 SP1 and 4.0 Silverlight 3, 4, and 5 XNA 4.0 for Xbox 360 Windows Phone 7 JavaScript (RxJS) Download at MSDN Data Developer Center or use NuGet

12 Environment MoveNext Got next? Application OnNext Have next! IEnumerable IEnumerator IEnumerable IEnumerator IObservable IObserver IObservable IObserver Interactive Reactive Push-Based Data Retrieval

13 Event Programming Interfaces interface IObservable { IDisposable Subscribe(IObserver observer); } interface IObserver { void OnNext(T value); void OnError(Exception ex); void OnCompleted(); } Both interfaces ship in the.NET 4 BCL

14 Fixed (MSIL) Translatable (Expression trees) ToQueryable ToObservable ToEnumerable AsQueryable AsEnumerable AsQbservable AsObservable Pull (interactive) Push (reactive) Concurrency (IScheduler) LINQ to *.* What? Where? How? Worker pools Message loops Threads Distributed Duality Homo-iconic IQbservable LINQ to WMI Events ToQbservable IQueryable LINQ to SQL IEnumerable LINQ to Objects IObservable LINQ to Events

15 IQbservable – The Dual of IQueryable We welcome semantic discussions Extended role for some operators No Execute method interface IQbservable : IObservable { Expression Expression { get; } Type ElementType { get; } IQbservableProvider Provider { get; } } interface IQbservableProvider { IQbservable CreateQuery (Expression expression); }

16 DEMO LINQ to WMI Events (WQL)

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

18 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 IAsyncEnumerable { IAsyncEnumerator GetEnumerator(); } public interface IAsyncEnumerator : IDisposable { T Current { get; } Task MoveNext(); } Can await Rx defines Query Operators

19 DEMO Rx support for C# and VB “await”

20 Where Execution Happens – Introducing Schedulers Rx’s unified way to introduce concurrency IEnumerable IObservable SynchronousAsynchronous ToObservable ToEnumerable interface IScheduler { DateTimeOffset Now { get; } IDisposable Schedule (T state, Func action); IDisposable Schedule (T state, TimeSpan dueTime, Func action); IDisposable Schedule (T state, DateTimeOffset dueTime, Func action); }

21 Imported TextBox TextChanged event Asynchronous web service call Use of scheduler to synchronize res.ObserveOn(new ControlScheduler(frm)).Subscribe(words => { lst.Items.Clear(); lst.Items.AddRange((from word in words select word.Word).ToArray()); }); res.Subscribe(words => { lst.Items.Clear(); lst.Items.AddRange((from word in words select word.Word).ToArray()); }); Indicates where things run IScheduler Specifies Where Things Happen var res = from word in input.DistinctUntilChanged().Throttle(TimeSpan.FromSeconds(0.5)) from words in lookup(word) select words;

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

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

24 DEMO Remoting of Query Expressions

25 LINQ to the Unexpected Model[ 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 ] from m in ctx.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 >= 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 Non-persistent Cost / barrel Max # barrels Max # barrels Min # barrels Min # barrels Refinement %

26 LINQ to the Unexpected from costSA in GetPriceMonitor("SA") from costVZ in GetPriceMonitor("VZ") from m in ctx.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 >= 500 where 0 <= m.sa && m.sa <= 9000 && 0 <= m.vz && m.vz <= 6000 orderby costSA * m.sa + costVZ * m.vz select m Observable data sources SelectMany Parameters to decision Subscribe here!

27 DEMO Theorem Solving using Z3

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

29 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 http://blogs.msdn.com/belux Join us on Facebook http://www.facebook.com/msdnbe http://www.facebook.com/msdnbelux LinkedIn: http://linkd.in/msdnbelux/ Twitter: @msdnbelux Download MSDN/TechNet Desktop Gadget http://bit.ly/msdntngadget

30 TechDays 2011 On-Demand Watch this session on-demand via Channel9 http://channel9.msdn.com/belux Download to your favorite MP3 or video player Get access to slides and recommended resources by the speakers

31 THANK YOU


Download ppt "LINQ, Take Two Realizing the LINQ to Everything Dream Bart J.F. De Smet blogs.bartdesmet.net/bart"

Similar presentations


Ads by Google