Syntax Flashcards

1
Q

Filtering

A
Query Syntax
-------------
var col = from o in Orders
 where o.CustomerID == 84
 select o;

var col2 = Orders.Where(o => o.CustomerID == 84);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Return Anonymous Type

A
Query Syntax
-------------
var col = from o in orders
 select new
 {
 OrderID = o.OrderID,
 Cost = o.Cost
 };
Lambda Syntax
-------------
var col2 = orders.Select(o => new
 {
 OrderID = o.OrderID,
 Cost = o.Cost
 }
 );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Ordering

A
>> Ascending
Query Syntax
-------------
var col = from o in orders
 orderby o.Cost ascending
 select o;

var col2 = orders.OrderBy(o => o.Cost);

>> Descending
Query Syntax
-------------
var col3 = from o in orders
 orderby o.Cost descending
 select o;

var col4 = orders.OrderByDescending(o => o.Cost);

Query Syntax
-------------
var col9 = from o in orders
 orderby o.CustomerID, o.Cost descending
 select o;
//returns same results as above
var col5 = from o in orders
 orderby o.Cost descending
 orderby o.CustomerID
 select o;

var col6 = orders.OrderBy(o => o.CustomerID).
ThenByDescending(o => o.Cost);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Joining

A
Query Syntax
-------------
var col = from c in customers
 join o in orders on
 c.CustomerID equals o.CustomerID
 select new
 {
 c.CustomerID,
 c.Name,
 o.OrderID,
 o.Cost
 };
Lambda Syntax
-------------
var col2 = customers.Join(orders,
 c => c.CustomerID,o => o.CustomerID,
 (c, o) => new
 {
 c.CustomerID,
 c.Name,
 o.OrderID,
 o.Cost
 }
 );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Grouping

A
Query Syntax
-------------
var OrderCounts = from o in orders
 group o by o.CustomerID into g
 select new
 {
 CustomerID = g.Key,
 TotalOrders = g.Count()
 };
Lambda Syntax
-------------
var OrderCounts1 = orders.GroupBy(
 o => o.CustomerID).
 Select(g => new
 {
 CustomerID = g.Key,
 TotalOrders = g.Count()
 });
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Paging

A

> > > select top 3

Query Syntax
-------------
var col = (from o in orders
 where o.CustomerID == 84
 select o).Take(3);
Lambda Syntax
-------------
var col2 = orders.Where(
 o => o.CustomerID == 84
 ).Take(3);

> > > skip first 2 and return the 2 after

Query Syntax
-------------
var col3 = (from o in orders
 where o.CustomerID == 84
 orderby o.Cost
 select o).Skip(2).Take(2);
Lambda Syntax
-------------
var col3 = (from o in orders
 where o.CustomerID == 84
 orderby o.Cost
 select o).Skip(2).Take(2);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Element Operators (throws exception if no elements)

A
Query Syntax
-------------
var cust = (from c in customers
 where c.CustomerID == 84
 select c).Single();

var cust1 = customers.Single(
c => c.CustomerID == 84);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Element Operators (returns null if no elements)

A
Query Syntax
-------------
var cust = (from c in customers
 where c.CustomerID == 84
 select c).SingleOrDefault();

var cust1 = customers.SingleOrDefault(
c => c.CustomerID == 84);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Element Operators (returns a new customer instance if no elements)

A
Query Syntax
-------------
var cust = (from c in customers
 where c.CustomerID == 85
 select c).DefaultIfEmpty(
 new Customer()).Single();
Lambda Syntax
-------------
var cust1 = customers.Where(
 c => c.CustomerID == 85
 ).DefaultIfEmpty(new Customer()).Single();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Element Operators (First, Last and ElementAt used in same way)

A
Query Syntax
-------------
var cust4 = (from o in orders
 where o.CustomerID == 84
 orderby o.Cost
 select o).Last();
Lambda Syntax
-------------
var cust5 = orders.Where(
 o => o.CustomerID == 84).
 OrderBy(o => o.Cost).Last();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Element Operators (returns 0 if no elements)

A
Query Syntax
-------------
var i = (from c in customers
 where c.CustomerID == 85
 select c.CustomerID).SingleOrDefault();
Lambda Syntax
-------------
var j = customers.Where(
 c => c.CustomerID == 85).
 Select(o => o.CustomerID).SingleOrDefault();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

ToArray

A

string[] names = (from c in customers

select c.Name).ToArray();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

ToDictionary

A

Dictionary col = customers.ToDictionary(c => c.CustomerID);

Dictionary customerOrdersWithMaxCost = (from oc in
(from o in orders
join c in customers on o.CustomerID equals c.CustomerID
select new { c.Name, o.Cost })
group oc by oc.Name into g
select g).ToDictionary(g => g.Key, g => g.Max(oc => oc.Cost));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

ToList

A

List ordersOver10 = (from o in orders
where o.Cost > 10
orderby o.Cost).ToList();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly