This is my SQL query:
select
m.Name, s.Time, t.TheaterNumber
from
Movies m
join
MovieSeanceTheaters mst on mst.MovieId = m.MovieID
join
Theaters t on t.ID = mst.TheaterId
join
Seances s on mst.SeanceId = s.ID
This is my attempt at a Linq query:
var result = (from m in _context.Movies
join mst in _context.MovieSeanceTheaters on m.ID equals mst.MovieId
join t in _context.Theaters on mst.TheaterId equals t.ID
join s in _context.Seances on mst.TheaterId equals s.ID
select new { Film = m.Name, Salon = t.Name, Seans = s.Time }
).ToList();
I made this attempt, but I want to make with lambda for instance:
var result = movieManager.GetAll().Where(x => x.MovieSeanceTheaters)....
I couldn't do that.
If I understand you correctly, you want to rewrite your query from query syntax to method syntax?
Here we are!
var result = _context.Movies
.Join(_context.MovieSeanceTheaters,
m => m.MovieID,
mst => mst.MovieID,
(m, mst) => new
{
m = m,
mst = mst
})
.Join(_context.Theaters,
temp0 => temp0.mst.TheaterID,
t => t.ID,
(temp0, t) =>
new
{
temp0 = temp0,
t = t
})
.Join(_context.Seances,
temp1 => temp1.temp0.mst.TheaterID,
s => s.ID,
(temp1, s) =>
new
{
Film = temp1.temp0.m.Name,
Salon = temp1.t.TheaterNumber,
Seans = s.Time
});
Looks ugly, doesn't it?
Most often, the method syntax is more compact and convenient. But in this case, leave it as is.
Related
I have a problem to write in lambda query this sql query:
select c.Id, c.Name, c.SomeNumber, count(*) from TableA a
inner join TableB b
on a.Id = b.aId
inner join TableC c
on c.BId = b.Id
where b.Status = N'Approved'
and c.Scope = N'Scope1'
group by a.Id, a.Name, a.SomeNumber
Can you guys help me with this one ? I want to write lambda query to execute this in code. I'm using EF Core 3.1
This is what I ended up so far:
var query = await _dbContext.TableA.Where(a => a.TableB.Any(b => b.Status.Equals("Approved")
&& b.TableC.Any(c => c.Scope.Equals("Scope1"))))
.GroupBy(g => new { Id = g.Id, Name = g.Name, SomeNumber = g.SomeNumber })
.Select(s => new { Id = s.Key.Id, Name = s.Key.Name, SomeNumber = s.Key.SomeNumber, Count = s.Count() })
.GroupBy(g => g.Id).Select(s => new {Id = s.Key, Count = s.Count()}).ToListAsync();
Well, this is corrected query. I have used Query syntax which is more readable when query has lot of joins or SelectMany.
var query =
from a in _dbContext.TableA
from b in a.TableB
from c in b.TableC
where b.Status == "Approved" && c.Scope == "Scope1"
group a by new { a.Id, a.Name, a.SomeNumber } into g
select new
{
g.Key.Id,
g.Key.Name,
g.Key.SomeNumber,
Count = g.Count()
}
var result = await query.ToListAsync();
It's maybe easier to start from the many end and work up through the navigation properties
tableC
.Where(c => c.Scope == "Scope1" && c.BEntity.Status == "Approved")
.GroupBy(c => new
{
c.BEntity.AEntity.Id,
c.BEntity.AEntity.Name,
c.BEntity.AEntity.SomeNumber
})
.Select(g => new { g.Key.Id, g.Key.Name, g.Key.SomeNumber, Ct = g.Count()})
EF knows how to do joins when you navigate around the object tree in the where. By starting at the many and and working up to the 1 end of the relationship it means you don't have to get complex with asking "do any of the children of this parent have a status of ..."
I have these two tables: Category (Parent) and Product (Children).
I could use inner join with LINQ query syntax. But with method syntax, I could not get. p. doesn't load CategoryID
var db = new NorthwindEntities();
var cats = db.Categories;
var prods = db.Products;
var catProducts1 = from c in db.Categories
join p in db.Products
on c.CategoryID equals p.CategoryID
select new { c.CategoryName, p.ProductName };
var catProducts2 = db.Categories
.Join(db.Products, c=>c.CategoryID,p=>p);
Below is the correct syntax of Join lamda syntax for your scenario:
var catProducts2 = db.Categories.Join(db.Products,
c => c.CategoryId,
p => p.CategoryID,
(c, p) => new { c.CategoryName, p.ProductName })
.Select(s => new { s.CategoryName, s.ProductName });
You can check the usage of above query at fiddle -- https://dotnetfiddle.net/C3N2I2
I Got This Lambda Expression But Does Not Work Correctly.Does Not Return Any Thing.Would You Help me Please on this:
var query = db.Cheque
.Join(db.Contracts,
C => C.ContractIDRef,
Con => Con.ContractID,
(C, Con) => new { Cheques1 = C, Contracts1 = Con })
.Join(db.Parties,
Con => Con.Contracts1.ContractID,
Pt => Pt.ContractIDRef,
(Con, Pt) => new { Contract2 = Con, Parites1 = Pt })
.Join(db.Persons,
Pt => Pt.Parites1.PartyIDRef,
P => P.PersonID,
(Pt, P) => new { Parites2 = Pt, Persons1 = P })
.Join(db.Company,
Pt => Pt.Parites2.Parites1.CompanyIDRef,
Com => Com.CompanyID,
(Pt, Com) => new { Parites3 = Pt, Company1 = Com })
.Join(db.Bank,
C => C.Parites3.Parites2.Contract2.Cheques1.BankIDRef,
B => B.BankID,
(C, B) => new { Cheque2 = C, Bank1 = B })
.Join(db.Flats,
Con => Con.Cheque2.Parites3.Parites2.Contract2.Contracts1.FlatIDRef,
F => F.FlatID,
(Con, F) => new { Contract3 = Con, Flat1 = F })
.Join(db.Projects,
F => F.Flat1.ProjectIDRef,
Pr => Pr.ProjectID,
(F, Pr) =>
new
{
ChequeNumber = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeNo,
ChequeIDRef = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeIDRef,
ChequePrice = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.Amount,
BankName = F.Contract3.Bank1.BankName,
BranchName = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.BranchName,
ChequeDate = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeDate,
AccountNumber = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.AccNo,
AccountOwner = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeOwnerName,
}
)
`.Where(Total => SelectedChequesList.Contains(Total.ChequeIDRef.Value)).ToList();
I will start with converting the above to the query syntax. While I'm a fan of the method syntax, using it in a complex queries involving multiple joins is a real pain. I needed a lot of time to read and try to follow the above query, so let do that:
var query =
from cheque in db.Cheque
join contract in db.Contracts on cheque.ContractIDRef equals contract.ContractID
join party in db.Parties on contract.ContractID equals party.ContractIDRef
join person in db.Persons on party.PartyIDRef equals person.PersonID
join company in db.Companies on party.CompanyIDRef equals company.CompanyID
join bank in db.Bank on cheque.BankIDRef equals bank.BankID
join flat in db.Flats on contract.FlatIDRef equals flat.FlatID
join project in db.Projects on flat.ProjectIDRef equals project.ProjectID
where SelectedChequesList.Contains(cheque.ChequeIDRef.Value)
select new
{
ChequeNumber = cheque.ChequeNo,
ChequeIDRef = cheque.ChequeIDRef,
ChequePrice = cheque.Amount,
BankName = bank.BankName,
BranchName = cheque.BranchName,
ChequeDate = cheque.ChequeDate,
AccountNumber = cheque.AccNo,
AccountOwner = cheque.ChequeOwnerName,
};
Now, one thing that can be seen is that most of the joins are not used. But let assume they are needed for some reason. Note that all the joins are INNER joins, so any of them can cause the query to return empty result if there is no matching record.
The problem is most probably in this join
join person in db.Persons on party.PartyIDRef equals person.PersonID
Instead of PartyIDRef I guess it should be something like PersonIDRef.
I have an SQL expression
select S.SpecialtyName, COUNT(distinct SUC.SiteUserId) as Subscribers
from SiteUserContent SUC Inner join
Specialties S on SUC.SpecialtyId = S.SpecialtyId Inner join
SiteUser SU on SUC.SiteUserId = SU.SiteUserId
where SU.DeletedFlag = 0
group by S.SpecialtyName
Order by S.SpecialtyName
What will be the corresponding LINQ expression for the same?
from suc in context.SiteUserContent
join s in context.Specialties on suc.SpecialtyId equals s.SpecialtyId
join su in context.SiteUser on suc.SiteUserId equals su.SiteUserId
where su.DeletedFlag == 0
select new { suc.SiteUserId, s.SpecialityName } into x
group x by x.SpecialityName into g
orderby g.Key
select new {
SpecialityName = g.Key,
Subscribers = g.Select(i => i.SiteUserId).Distinct().Count()
}
Generated SQL will not be same, but I think result of query execution should be same.
var results = contex.SiteUserContent
.Join(context.Specialties, suc => suc.SpecialtyId, s => s.SpecialtyId, (suc, s) => new { suc, s })
.Join(context.SiteUser, i = i.suc.SiteUserId, su => su.SiteUserId, (i, su) => new { suc = i.suc, s = i.s, su = su })
.Where(i => i.su.DeletedFlag == 0)
.GroupBy(i => i.s.SpecialtyName)
.Select(g => new {
SpecialityName = g.Key,
Subscribers = g.Select(i => i.suc.SiteUserId)
.Distinct()
.Count()
})
.OrderBy(i => i.SpecialityName);
I have some SQL and am trying to make the equivalent in LINQ. This is the SQL:
SELECT Categories.CategoryDescription, Categories.CategoryType AS Type,
Categories.Category, COUNT(CategoryLinks.OrgID) AS CountOfOrgs
FROM CategoryLinks
INNER JOIN Categories ON Categories.CategoryID = CategoryLinks.CategoryID
GROUP BY Categories.Category, Categories.CategoryType, Categories.CategoryDescription
ORDER BY CategoryDescription ASC
Essentially, I want a list of everything from the Categories table and a count of the number of OrgId's in the CategoryLinks table that links to it.
Below is the query I am performing at the moment. There has to be a more efficient way to do this. Am I wrong?
var cnts = (from c in db.Categories
join cl in db.CategoryLinks on c.CategoryID equals cl.CategoryID
group new { c, cl } by new
{
c.CategoryID
} into g
select new
{
CategoryID = g.Key.CategoryID,
categoryCount = g.Count()
});
var results = (from c in db.Categories
join cn in cnts on c.CategoryID equals cn.CategoryID
select new
{
c.CategoryID,
c.CategoryDescription,
c.CategoryType,
Category = c.Category1,
cn.categoryCount
});
I think you want to use the GroupJoin method:
Categories.GroupJoin(
CategoryLinks,
x => x.CategoryID,
y => y.CategoryID,
(x,y) => new{
x.CategoryID,
x.CategoryDescription,
x.CategoryType,
Category = x.Category1,
CategoryCount = y.Count() })
In query syntax, this is written as join..into:
from c in db.Categories
join cl in db.CategoryLinks on c.CategoryID equals cl.CategoryID into catGroup
select new
{
c.CategoryID,
c.CategoryDescription,
c.CategoryType,
Category = c.Category1,
CategoryCount = catGroup.Count()
}
Try this:
var bbb = categories.Join(categoryLinks, c => c.CategoryID, cl => cl.CategoryId, (c, cl) => new {c, cl})
.GroupBy(g => g.c)
.Select(g => new {count = g.Count(), Category = g.Key});
It returns count and all data that is in Category. We group by all columns in category and place result in new anonymous type variable that contains 2 properties: Count, that contains count and Category that is of type Category and contains all data that is in category row.
If you want, you can rewrite it as:
var bbb = categories.Join(categoryLinks, c => c.CategoryID, cl => cl.CategoryId, (c, cl) => new {c, cl})
.GroupBy(g => g.c)
.Select(g => new
{
CategoryID = g.Key.CategoryId,
CategoryDescription = g.Key.CategoryDescription,
CategoryType = g.Key.CategoryType,
Category = g.Key.Category1,
categoryCount = g.Count()
});