Bnei Brak 51202 IsraelParallel LINQ Notes... Bnei Brak 51202 IsraelDesigner Support – Can work objects-first or schema-first... Bnei Brak 51202 IsraelUsing LINQ to Entities using SalesEn
Trang 1LINQ via C# 3.0
Chapter 7 – Beyond LINQ
Trang 2© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Trang 4© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Parallel LINQ Notes
Trang 5ADO.NET Entity Framework
– Business entities – Conceptual model – Relational data
– Single class multiple tables – Many-to-many relationships – Arbitrary queries (Entity-SQL) – Will support providers other than SQL Server®
Trang 6© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Designer Support
– Can work objects-first or schema-first
Trang 7[DataContract(IsReference = true), Serializable]
public partial class SaleInfo : EntityObject {
Trang 8© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Using LINQ to Entities
using (SalesEntities context = new
SalesEntities()) {
var everyone =
context.CreateQuery<SalesPerson>(
"SELECT VALUE sp FROM SalesPeople AS sp" );
foreach (var p in everyone)
Amount = g.Sum(s => s.Amount) };
foreach (var info in
Trang 10© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Entity Framework vs LINQ to SQL
– It’s not just a RAD drag-and-drop tool – Moderate learning curve
– Steep learning curve
– Discussion: http://tinyurl.com/5wa5qk,
http://tinyurl.com/573jo8, http://tinyurl.com/6dxeej
Trang 11ADO.NET Data Services
ATOM or JSON, REST
– http://example.com/Orders/Order(15) – http://example.com/Orders/BiggestOrders?n=5 – http://example.com/Orders/Order(15)/Products
Trang 12© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Data Services
– T can be LINQ to SQL data context
– T can be Entity Framework model
DataServiceHostFactory
Trang 14© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Out Of The Box
Trang 15Out Of The Box
Trang 16© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Out Of The Box
Trang 17Service Operations
[WebGet]
public IQueryable<SaleInfo> TopSales(int n) {
return (from sale in
information)
Trang 18© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Service Operations
[WebGet]
public IQueryable<SaleInfo> TopSales(int n) {
return (from sale in
this CurrentDataSource.Sales
orderby sale.Amount descending
select sale).Take(n);
}
Trang 19Client Code
System.Data.Services.Client.dll
– And the companion DataSvcUtil.exe tool
– (Works with a conceptual model directly as well!)
– Can work with shared data contracts – Can generate only the contracts we need – WCF SP1 features support for POCO (don’t need
[DataContract] or [DataMember] on anything)
– Can work with DataSvcUtil-generated classes
Trang 20© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Client Code – Manual
class SaleInfo { //POCO
public int Id { get; set; } //Plain Old CLR Object
public int Amount { get; set; }
}
Trang 21Client Code – Generated
public partial class SalesEntities :
Trang 22© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Client Code – Generated
SalesEntities context2 = new SalesEntities(
• The query is effectively “serialized”
• A small subset of query operators is supported
• Most work has to be done on the client
Trang 23Data Services Over LINQ to SQL
• Any DataContext-assignable class can
• Table<T> does not implement
IUpdatable
– This means that insert, delete and update operations
over the client context will fail
– http://tinyurl.com/5ranan
Trang 24© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Data Services – Quirks with LINQ to SQL
foreign keys) don’t work by default
– A primary key is required
– Resolve with [DataServiceKey(“FK1”, “FK2”)]
on the generated class (partial)
don’t work by default
– The [DataServiceKey] attribute is required
Trang 25Custom LINQ Providers
IEnumerable or the query pattern
– Parsing the query expression tree – Translating it to SQL (e.g LINQ to Oracle), web service calls (e.g LINQ to Amazon), other system APIs (e.g LINQ to WMI)
– List of providers: http://tinyurl.com/yt8ala
Trang 26© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel
Some Custom LINQ Providers
• LINQ to… Your Own!
Trang 27Chapter Summary
Trang 28© Copyright SELA Software & Education Labs Ltd 14-18 Baruch Hirsch St Bnei Brak 51202 Israel