© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
LINQ via C# 3.0
Chapter 7 – Beyond LINQ
Beyond LINQ 1LINQ C#3
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
What’s Beyond?
•
Very near:
•
/>–
ADO.NET Entity Framework (VS2008 SP1 Beta1)
•
/>–
ADO.NET Data Services (VS2008 SP1 Beta1)
•
•
Quite near:
–
Parallel LINQ (June08 CTP)
•
/>•
LINQ to Your Own
Beyond LINQ 2LINQ C#3
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Parallel LINQ
•
Part of the Parallel Extensions for .NET
–
System.Threading.dll assembly
–
Adds AsParallel() extension method to
IEnumerable
–
Currently supports LINQ to Objects only
from n
in Enumerable.Range(...).AsParallel()
where IsPrime(n)
select n
Beyond LINQ 3LINQ C#3
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Parallel LINQ Notes
•
Current CTP is stable but there are still
performance issues
•
Parallelizing tiny inputs harms
performance
•
Can specify degree of parallelism to
AsParallel()
•
Can specify order preservation (pipeline
processing)
Beyond LINQ 4LINQ C#3
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
ADO.NET Entity Framework
•
Advanced Object-Relational Mapper
•
Supports three-tiered mapping:
–
Business entities
–
Conceptual model
–
Relational data
•
More flexible than LINQ to SQL:
–
Single class multiple tables
–
Many-to-many relationships
–
Arbitrary queries (Entity-SQL)
–
Will support providers other than SQL Server®
Beyond LINQ 5LINQ C#3
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Designer Support
•
Modeling very similar to LINQ to SQL
–
Can work objects-first or schema-first
Beyond LINQ 6LINQ C#3
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Generated Classes
public partial class SalesEntities : ObjectContext {
...
public ObjectQuery<SaleInfo> Sales ...
}
[EdmEntityType(..., Name=“SaleInfo”)]
[DataContract(IsReference = true), Serializable]
public partial class SaleInfo : EntityObject {
...
[EdmScalarProperty(EntityKeyProperty=true, ...)]
[DataMember]
public int Id ...
}
Beyond LINQ 7LINQ C#3
© 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)
Console.WriteLine(p.Name + " " + p.Seniority);
var query =
from sale in context.Sales
where sale.Amount > 5
group sale by sale.SalesPerson.Name into g
select new {
Name = g.Key,
Amount = g.Sum(s => s.Amount) };
foreach (var info in
query.OrderByDescending(i => i.Amount))
Console.WriteLine(info.Name + ": " + info.Amount);
}
Entity-SQL
Beyond LINQ 8LINQ C#3
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
More Entity-SQL
•
Navigating relationships:
var highSales =
context.CreateQuery<SalesPerson>(
@"SELECT VALUE sp FROM SalesPeople AS sp
WHERE EXISTS (
SELECT VALUE s
FROM NAVIGATE(sp, SM.SalesPeople_Sales) AS s
WHERE s.Amount > 50
)");
foreach (var p in highSales)
Console.WriteLine(p.Name + " " +
p.Sales.Sum(s => s.Amount));
Beyond LINQ 9LINQ C#3
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Entity Framework vs. LINQ to SQL
•
LINQ to SQL is a full ORM
–
It’s not just a RAD drag-and-drop tool
–
Moderate learning curve
•
Entity Framework is fuller
–
Steep learning curve
•
Most projects can use LINQ to SQL
–
Discussion:
/>Beyond LINQ 10LINQ C#3
© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
ADO.NET Data Services
•
LINQ: Data as Objects
•
Astoria: Data as Services
•
Exposing data sources through URIs,
ATOM or JSON, REST
–
/>–
/>–
/>Beyond LINQ 11LINQ C#3