Actor Name
GEN
LI
. This time, order the rows by last name and first name, in that orderIN
, display the country_id
and country
columns of the following countries: Afghanistan, Bangladesh, and ChinaHARPO WILLIAMS
was accidentally entered in the actor table as GROUCHO WILLIAMS. Write a query to fix the recordJOIN
to display the total amount rung up by each staff member in August of 2005. Use tables staff
and payment
K
and Q
have also soared in popularity. Use subqueries to display the titles of movies starting with the letters K
and Q
whose language is EnglishAlone Trip
.We omitted the most trivial questions and DDL.
Actor Name
var query = DbContext.ActorNames.Query((Actor actor, ActorName alias) =>
{
var result = SELECT<ActorName>((actor.FirstName + " " + actor.LastName).ToUpper().@as(alias.FullName));
FROM(actor);
return result;
});
foreach (var actorName in query.Take(3))
Console.WriteLine(actorName.FullName);
... using C# interpolation syntax:
var query = DbContext.ActorNames.Query((Actor actor, ActorName alias) =>
{
var result = SELECT<ActorName>($"{actor.FirstName} {actor.LastName}".ToUpper().@as(alias.FullName));
FROM(actor);
return result;
});
foreach (var actorName in query.Take(3))
Console.WriteLine(actorName.FullName);
var name = "Joe";
var query = DbContext.Actor.Query((Actor actor) =>
{
var result = SELECT(actor);
FROM(actor);
WHERE(actor.FirstName.ToLower() == name.ToLower());
return result;
});
foreach (var actor in query.Take(3))
Console.WriteLine((actor.ActorId, actor.FirstName, actor.LastName, actor.LastUpdate));
GEN
var letters = "GEN";
var query = DbContext.Actor.Query((Actor actor) =>
{
var result = SELECT(actor);
FROM(actor);
WHERE(actor.LastName.ToUpper().Contains(letters));
return result;
});
foreach (var actor in query.Take(3))
Console.WriteLine((actor.ActorId, actor.FirstName, actor.LastName, actor.LastUpdate));
LI
. This time, order the rows by last name and first name, in that order
var letters = "LI";
var query = DbContext.Actor.Query((Actor actor) =>
{
var result = SELECT(actor);
FROM(actor);
WHERE(actor.LastName.ToUpper().Contains(letters));
ORDER(BY(actor.LastName), BY(actor.FirstName));
return result;
});
foreach (var actor in query.Take(3))
Console.WriteLine((actor.ActorId, actor.FirstName, actor.LastName, actor.LastUpdate));
IN
, display the country_id
and country
columns of the following countries: Afghanistan, Bangladesh, and China
string[] countries = { "China", "Afghanistan", "Bangladesh" };
var query = DbContext.Country.Query((Country country) =>
{
var result = SELECT(country);
FROM(country);
WHERE(countries.Contains(country.Name));
return result;
});
foreach (var country in query.Take(3))
Console.WriteLine((country.CountryId, country.Name, country.City, country.LastUpdate));
var query = DbContext.ActorNameCounts.Query((Actor actor, ActorNameCount alias) =>
{
var result = SELECT<ActorNameCount>(actor.LastName.@as(alias.LastName), COUNT().@as(alias.Count));
FROM(actor);
GROUP(BY(actor.LastName));
ORDER(BY(actor.LastName).DESC);
return result;
});
foreach (var actor in query.Take(3))
Console.WriteLine((actor.LastName, actor.Count));
var query = DbContext.ActorNameCounts.Query((Actor actor, ActorNameCount alias) =>
{
var actorCount = COUNT();
var result = SELECT<ActorNameCount>(actor.LastName.@as(alias.LastName), actorCount.@as(alias.Count));
FROM(actor);
GROUP(BY(actor.LastName));
HAVING(actorCount > 1);
ORDER(BY(actorCount).DESC, BY(actor.LastName));
return result;
});
foreach (var actor in query.Take(3))
Console.WriteLine((actor.LastName, actor.Count));
HARPO WILLIAMS
was accidentally entered in the actor table as GROUCHO WILLIAMS. Write a query to fix the record
var newFirstName = "HARPO";
var oldFirstName = "GROUCHO";
var lastName = "WILLIAMS";
var query = DbContext.Actor.Query((Actor actor) =>
{
UPDATE(actor).SET(() => actor.FirstName = newFirstName);
WHERE(actor.FirstName == oldFirstName && actor.LastName == lastName);
Semicolon();
var result = SELECT(actor);
FROM(actor);
WHERE(actor.LastName == lastName);
return result;
})
.AsEnumerable();
foreach (var actor in query.Take(3))
Console.WriteLine((actor.ActorId, actor.FirstName, actor.LastName, actor.LastUpdate));
JOIN
to display the total amount rung up by each staff member in August of 2005. Use tables staff
and payment
var payMonth = 8;
var payYear = 2005;
var query = DbContext.StaffPayments.Query((Staff staff, Payment payment, StaffPayment alias) =>
{
var r = SELECT<StaffPayment>(staff.StaffId.@as(alias.Staff.StaffId), SUM(payment.Amount).@as(alias.Amount));
FROM(staff).LEFT_JOIN(payment).ON(staff == payment.Staff);
WHERE(MONTH(payment.PaymentDate) == payMonth && YEAR(payment.PaymentDate) == payYear);
GROUP(BY(staff.StaffId)); //must GROUP BY StaffId since this field is used in SELECT
return r;
})
.Include(sp => sp.Staff);
foreach (var staffPayment in query.Take(3))
Console.WriteLine((staffPayment.Staff.FirstName, staffPayment.Staff.LastName, staffPayment.Amount));
using DateTime
properties mappings:
var payMonth = 8;
var payYear = 2005;
var query = DbContext.StaffPayments.Query((Staff staff, Payment payment, StaffPayment alias) =>
{
var r = SELECT<StaffPayment>(staff.StaffId.@as(alias.Staff.StaffId), SUM(payment.Amount).@as(alias.Amount));
FROM(staff).LEFT_JOIN(payment).ON(staff == payment.Staff);
WHERE(payment.PaymentDate.Month == payMonth && payment.PaymentDate.Year == payYear);
GROUP(BY(staff.StaffId)); //must GROUP BY StaffId since this field is used in SELECT
return r;
})
.Include(sp => sp.Staff);
foreach (var staffPayment in query.Take(3))
Console.WriteLine((staffPayment.Staff.FirstName, staffPayment.Staff.LastName, staffPayment.Amount));
K
and Q
have also soared in popularity. Use subqueries to display the titles of movies starting with the letters K
and Q
whose language is English
var language = "English";
var query = DbContext.Film.Query((Film film) =>
{
var languages = SubQuery((Language lang) =>
{
var l = SELECT(lang.LanguageId);
FROM(lang);
WHERE(lang.Name == language);
return l.AsCollection();
});
var r = SELECT(film);
FROM(film);
WHERE((film.Title.StartsWith("K") || film.Title.StartsWith("Q")) && languages.Contains(film.Language.LanguageId));
return r;
});
foreach (var film in query.Take(3))
Console.WriteLine(film.Title);
Alone Trip
var filmName = "Alone Trip";
var query = DbContext.Actor.Query((Actor actor) =>
{
var films = SubQuery((Film film) =>
{
var l = SELECT(film.FilmId);
FROM(film);
WHERE(film.Title.ToLower() == filmName.ToLower());
return l.AsCollection();
});
var actors = SubQuery((FilmActor filmActor) =>
{
var l = SELECT(filmActor.ActorId);
FROM(filmActor);
WHERE(films.Contains(filmActor.FilmId));
return l.AsCollection();
});
var r = SELECT(actor);
FROM(actor);
WHERE(actors.Contains(actor.ActorId));
return r;
});
foreach (var actor in query.Take(3))
Console.WriteLine((actor.FirstName, actor.LastName));
same with ActorsByFilms
refactored out:
public void Test7B_1()
{
var filmName = "Alone Trip";
var query = DbContext.Actor.Query((Actor actor) =>
{
var films = SubQuery((Film film) =>
{
var l = SELECT(film.FilmId);
FROM(film);
WHERE(film.Title.ToLower() == filmName.ToLower());
return l.AsCollection();
});
var actors = ActorsByFilms(films);
var r = SELECT(actor);
FROM(actor);
WHERE(actors.Contains(actor.ActorId));
return r;
});
foreach (var actor in query.Take(3))
Console.WriteLine((actor.FirstName, actor.LastName));
}
private static ICollection<ushort> ActorsByFilms(ICollection<ushort> films)
{
return SubQuery((FilmActor filmActor) =>
{
var l = SELECT(filmActor.ActorId);
FROM(filmActor);
WHERE(films.Contains(filmActor.FilmId));
return l.AsCollection();
});
}