var model = _db.Seuni
.Join(from o in _db.Keuni
join
c in _db.Seuni
on o.klasaid equals c.klasaid
select new {o.emriklases,c.emristudent,c.studentid,c.nota })
.OrderByDescending(r => r.nota)
I have this sample of code,I have searched to find the right syntax for this linq extension method ,and I haven't can sb give to me the right one?Thank You in advance
You have mixed query and method syntax in a wrong way.
var model = _db.Seuni
.Join(_db.Keuni,
c => c.klasaid,
o => o.klasaid,
(c, o) => new { o.emriklases, c.emristudent, c.studentid, c.nota })
.OrderByDescending(r => r.nota);
Or with query (method for order) syntax
// query syntax
var model = (from o in _db.Keuni
join c in _db.Seuni on o.klasaid equals c.klasaid
select new { o.emriklases, c.emristudent, c.studentid, c.nota })
.OrderByDescending(r => r.nota);
Related
How to nested parsing expression ,I'm developing an ORM, who can help me with some ideas?
var list = db.Queryable<Student>()
.Where(it => it.Id.Equals(db.Queryable<School>()
.Where(sc => sc.Id == it.Id&&sc.Name=="jack"))).ToList()
It's batter to use LINQ joins for this purpose.
Example:
1| LINQ query-syntax
var list = (from s in db.Student
join sc in db.School on s.SchoolId equals sc.Id
where sc.Name=="jack"
select s).ToList() ;
2| LINQ Extension method
var list = db.Student.Join(db.School,
s => s.SchoolId,
sc => sc.Id,
(s, sc) => new { s, sc })
.Where(ss => ss.sc.Name=="jack")
.Select(ss => ss.s).ToList();
You can get more information about query-syntax and Extension method in this answer.
How do I convert this query to a LINQ query in Entity Framework? I am a new programmer still in school
SELECT
studfirstname, studlastname, grade
FROM
students
INNER JOIN
Student_Schedules ON students.StudentID = Student_Schedules.StudentID
INNER JOIN
Classes ON Student_Schedules.ClassID = Classes.ClassID
INNER JOIN
Subjects ON Classes.SubjectID = Subjects.SubjectID
WHERE
SubjectName = 'Introduction to Art';
As you are new in Lambda and LINQ I tried in the following Lambda to be as descriptive as possible..... You can use the Long Aliases in short form also
student_schedules as ss OR studentsANDstudent_schedules as s_s_sch... Try this ...Hope this helps.... #Abdul Hameed
var filteredData = context.Students // your starting point
.Join(context.Student_Schedules, // the source table of the inner join
students => students.StudentID, // Select the primary key (the first part of the "on" clause in an sql "join" statement)
student_schedules => student_schedules.StudentID),// the foreign key
(students, student_schedules) => new { students, student_schedules })
.Join(context.Classes,
classes => classes.ClassID,
studentsANDstudent_schedules => studentsANDstudent_schedules.student_schedules.ClassID),
(classes, studentsANDstudent_schedules) => new { classes, studentsANDstudent_schedules })
.Join(context.Subjects,
subjects => subjects.SubjectID,
studentsANDstudent_schedulesANDClasses => studentsANDstudent_schedulesANDClasses.classes.SubjectID),
(subjects, studentsANDstudent_schedulesANDClasses) => new { subjects, studentsANDstudent_schedulesANDClasses })
.Where(allMergedTables => allMergedTables.subjects.SubjectName == "Intoduction to Art").ToList();
And this is the shortened version of the above one...
var filteredData = context.Students
.Join(context.Student_Schedules, s => s.StudentID, sch => sch.StudentID),
(s, sch) => new { s, sch })
.Join(context.Classes, c => c.ClassID, s_sch => s_sch.sch.ClassID),
(c, s_sch) => new { c, s_sch })
.Join(context.Subjects, sj => sj.SubjectID, s_sch_c => s_sch_c.c.SubjectID),
(sj, s_sch_c) => new { sj, s_sch_c })
.Where(all => all.sj.SubjectName == "Intoduction to Art")
.ToList();
You can use LINQ syntax:
from student in context.Students
join schedule in context.Student_Schedules on student.StudentID equals schedule.StudentID
join #class in context.Classes on schedule.ClassID equals #class.ClassID
join subject in context.Subjects on #class.SubjectID equals subject.SubjectID
where subject.SubjectName == "Introduction to Art"
var students = context.Students.Include("Student_Schedules")
.Include("Classes")
.Include("Subjects").Where(s => s.Subjects.Contains("Intoduction to Art"));
Info -> https://msdn.microsoft.com/en-us/data/jj574232.aspx
I want to do a simple subquery in LINQ to EF
I did something like this:
from p in db.SomeTable
let o = db.SomeTableWithDate
.OrderByDescending(t => t.Date)
.FirstOrDefault(lt => lt.SomeValue == value)
select new {p, o}
Everything compiles and LINQ isn't complaining, but the result is wrong.
The generated SQL is an OUTER APPLY with a TOP 1, but there is no 'ORDER BY'.
I also tried this:
from p in db.SomeTable
select new {
p,
o = db.SomeTableWithDate
.OrderByDescending(t => t.Date)
.FirstOrDefault(lt => lt.SomeValue == value)
}
But I get the same result. (I prefer 'let' because then I can use variables from the previous 'let' query)
So here is my question: how can i make LINQ do a real subquery with orderby?
I want to get the latest date from a linked table
Solution
The answer from boran solved it. I just had to do a seperate where first.
from p in db.SomeTable
let o = db.SomeTableWithDate
.Where(lt => lt.SomeValue == value)
.OrderByDescending(t => t.Date)
.FirstOrDefault()
select new {p, o}
from p in db.SomeTable
let o = db.SomeTableWithDate.Where(lt => lt.SomeValue == value)
.OrderByDescending(t => t.Date)
.FirstOrDefault()
select new {p, o}
Because you order after filtering, this query will probably have better performance too.
I have a simple LINQ lambda join query but I want to add a 3rd join with a where clause. How do I go about doing that?
Here's my single join query:
var myList = Companies
.Join(
Sectors,
comp => comp.Sector_code,
sect => sect.Sector_code,
(comp, sect) => new {Company = comp, Sector = sect} )
.Select( c => new {
c.Company.Equity_cusip,
c.Company.Company_name,
c.Company.Primary_exchange,
c.Company.Sector_code,
c.Sector.Description
});
I want to add the following SQL command to the above LINQ query and still maintain the projections:
SELECT
sector_code, industry_code
FROM
distribution_sector_industry
WHERE
service = 'numerical'
The 3rd join would be made with Sector table & Distribution_sector_industry on sector_code.
Thanks in advance.
Just a guess:
var myList = Companies
.Join(
Sectors,
comp => comp.Sector_code,
sect => sect.Sector_code,
(comp, sect) => new { Company = comp, Sector = sect })
.Join(
DistributionSectorIndustry.Where(dsi => dsi.Service == "numerical"),
cs => cs.Sector.Sector_code,
dsi => dsi.Sector_code,
(cs, dsi) => new { cs.Company, cs.Sector, IndustryCode = dsi.Industry_code })
.Select(c => new {
c.Company.Equity_cusip,
c.Company.Company_name,
c.Company.Primary_exchange,
c.Company.Sector_code,
c.Sector.Description,
c.IndustryCode
});
Okay, I can't see why you'd want to select sector_code when you already know it, but I think you want this:
var query = from company in Companies
join sector in Sectors
on company.SectorCode equals sector.SectorCode
join industry in DistributionSectorIndustry
on sector.SectorCode equals industry.SectorCode
where industry.Service == "numerical"
select new {
company.EquityCusip,
company.CompanyName,
company.PrimaryExchange,
company.SectorCode,
sector.Description,
industry.IndustryCode
};
Notes:
I've changed it into a query expression as that's a much more readable way of expressing a query like this.
Although the "where" clause comes after the join, assuming this is a LINQ to SQL or Entity Framework query, it shouldn't make any difference
I've lengthened the range variable names for clarity
I've converted your other names into conventional .NET names; you can do this too in your model
For 4 Tables
var query = CurrencyDeposits
.Join(Customers, cd => cd.CustomerId, cus => cus.Id, (cd, cus)
=> new { CurrencyDeposit = cd, Customer = cus })
.Join(Currencies, x => x.CurrencyDeposit.CurrencyId, cr => cr.Id, (x, cr)
=> new { x.CurrencyDeposit, x.Customer, Currency = cr })
.Join(Banks, x => x.CurrencyDeposit.BankId, bn => bn.Id, (x, bn)
=> new { x.CurrencyDeposit, x.Customer, x.Currency, Bank = bn})
.Select(s => new {
s.CurrencyDeposit.Id,
s.Customer.NameSurname,
s.Currency.Code,
s.Bank.BankName,
s.CurrencyDeposit.RequesCode
});
Try something like this...
var myList = ({from a in Companies
join b in Sectors on a.Sector_code equals b.Sector_code
join c in Distribution on b.distribution_code equals a.distribution_code
select new {...});
Does anyone know how to write this
var q = from c in customers
join o in orders on c.Key equals o.Key
select new {c.Name, o.OrderNumber};
In this syntax style?
var 1= customers.
.join(???)
.select(???)
I've been googling for a way to do this for days now with now luck. Everyone preferes the first syntax for tutorials, but I find the second much easier to determine order of operation when reading.
The compiler translation process involves using a "transparent identifier" that makes the current customer and order available to the Select method. You can emulate this by making your own:
customers.Join(orders, c => c.Key, o => o.Key, (c, o) => new { c, o })
.Select(x => new { x.c.Name, x.o.OrderNumber });
Instead, you could just move your Name/OrderNumber projection up into the Join call:
customers.Join(orders, c => c.Key, o => o.Key, (c, o) => new { c.Name, o.OrderNumber });
This just takes a single call to Enumerable.Join:
var q = customers.Join(
orders,
c => c.Key,
o => o.Key,
(c, o) => new {c.Name, o.OrderNumber}
);
You might also check out LinqPad . It has a small lambda button for the bottom half of the screen, and it translates the linq query to chain methods :
from p in PropertyListings
from rl in p.ResidentialListings
select new {p.YearBuilt,p.ListingUrl}
got translated to :
PropertyListings
.SelectMany (
p => p.ResidentialListings,
(p, rl) =>
new
{
YearBuilt = p.YearBuilt,
ListingUrl = p.ListingUrl
}
)