Beruflich Dokumente
Kultur Dokumente
ST Colloquium, 2008-05-15
Tom Lokhorst
Brief example
Brief example
Deconstructing expressions
Usages of LINQ
Objects
Relational
Assignment
Query comprehenssions
IEnumerable<T>
Its old
Its ugly
Its impractical
Document-centric
Document-centric
<Company name=Microsoft>
<Founders>
<Person>Bill Gates</Person>
</Founders>
</Company>
Document Object Model
XmlDocument doc = new XmlDocument();
XmlElement company = doc.CreateElement("Company");
doc.AppendChild(company);
XElement company =
new XElement("Company",
new XAttribute("name", "Microsoft"),
new XElement("Founders",
new XElement("Person", "Bill Gates")
)
);
XML with queries
XML with queries
string[] names = { Anders, Erik, Amanda };
XML with queries
string[] names = { Anders, Erik, Amanda };
from n in names
where n.StartsWith(A)
select new XElement(Person, n)
XML with queries
string[] names = { Anders, Erik, Amanda };
XElement employees =
new XElement(Employees,
from n in names
where n.StartsWith(A)
select new XElement(Person, n)
);
XML with queries
string[] names = { Anders, Erik, Amanda };
XElement employees =
new XElement(Employees,
from n in names
where n.StartsWith(A)
select new XElement(Person, n)
);
company.Add(employees);
XML with queries
string[] names = { Anders, Erik, Amanda };
XElement employees =
new XElement(Employees,
from n in names
where n.StartsWith(A)
select new XElement(Person, n)
);
company.Add(employees);
Console.WriteLine(company);
XML on the Console
<Company name=Microsoft>
<Founders>
<Person>Bill Gates</Person>
</Founders>
<Employees>
<Person>Anders</Person>
<Person>Amanda</Person>
</Employees>
</Company>
Querying XML
IEnumerable<string> persons =
from p in company.Descendants(Person)
select p.Value;
Querying XML
IEnumerable<string> persons =
from p in company.Descendants(Person)
select p.Value;
Bill Gates
Anders
Amanda
LINQ to SQL
Querying a SQL backend
[Column(Storage="_CustomerID",
DbType="NChar(5) NOT NULL",
CanBeNull=false, IsPrimaryKey=true)]
public string CustomerID
{
get { return this._CustomerID; }
set { /* Setter code removed */ }
}
var q = db.Customers.Where<Customer>(
c => c.City == London);
Lambdas again
Lambdas again
Func<int, int> f = x => (x + 1) * 2;
int nr = f(20);
Console.WriteLine(nr); // Prints: 42
Lambdas again
Expression<Func<int, int>> e = x => (x + 1) * 2;
Lambdas again
Expression<Func<int, int>> e = x => (x + 1) * 2;
Console.WriteLine(f(20)); // Prints: 42
Lambdas again
Expression<Func<int, int>> e = x => (x + 1) * 2;
Lambdas again
Expression<Func<int, int>> e = x => (x + 1) * 2;
ParameterExpression x =
Expression.Parameter(typeof(int), "x");
Expression one = Expression.Constant(1, typeof(int));
Expression two = Expression.Constant(2, typeof(int));
Expression body =
Expression.Multiply(Expression.Add(x, one), two);
Expression<Func<int, int>> e =
Expression.Lambda<Func<int, int>>(body, x);
LINQ to SQL example
var q = db.Customers.Where<Customer>(
c => c.City == London);
LINQ to SQL example
var q = db.Customers.Where<Customer>(
c => c.City == London);
ParameterExpression c =
Expression.Parameter(typeof(Customer), "c");
IQueryable<Customer> q = db.Customers
.Where<Customer>(Expression.Lambda<Func<Customer, bool>>(
Expression.Equal(
Expression.Property(c,
(MethodInfo) methodof(Customer.get_City)),
Expression.Constant("London", typeof(string)), false,
(MethodInfo) methodof(string.op_Equality)), c));
LINQ to SQL example
var q = db.Customers.Where<Customer>(
c => c.City == London);
SELECT C.*
FROM Customers AS C
WHERE C.City = London
Many more operations
int x = db.Customers.Where<Customer>(
c => c.City == London).Count();
int x = db.Customers.Where<Customer>(
c => c.City == London).Take(4);
LINQ to Google
LINQ to Entities
Parallel LINQ
Query comprehensions
Libraries
Expression trees
Monadic parser
Ident = from s in Id where s != "let" && s != "in" select s;
LetId = from s in Id where s == "let" select s;
InId = from s in Id where s == "in" select s;
combinators using C#
Term1 = (from x in Ident
select (Term)new VarTerm(x))
.OR(
(from u1 in WsChr('(')
from t in Term
from u2 in WsChr(')')
select t));
Term = (from u1 in WsChr('\\')
from x in Ident
from u2 in WsChr('.')
from t in Term
select (Term)new LambdaTerm(x,t))
.OR(
(from letid in LetId
from x in Ident
from u1 in WsChr('=')
from t in Term
from inid in InId
from c in Term
select (Term)new LetTerm(x,t,c)))
.OR(
(from t in Term1
from ts in Rep(Term1)
select (Term)new AppTerm(t,ts)));
All = from t in Term from u in WsChr(';') select t;
}