I'm working with Entity Framework Core. I'm trying to write a linq query which takes inputs from two tables SccmTable and Sailpoint table.
I want to satisfy these 3 conditions:
lcstate = deletedinsailpointtableUserNameinsailpointequalsUsernameinsccmtable- Select only rows with unique machine name
I tried this method
var result = (from r in DBContext.SccmTable
join s in DBContext.Sailpoint on r.UserName equals s.Username
where s.LcState == "DELETED"
select r).ToList()
.GroupBy(r => r.MachineName)
.Select(g => g.First());
And I also tried this code:
var result = DBContext.SccmTable
.Where(x => DBContext.Sailpoint
.Where(y => y.LcState == "DELETED")
.Select(y => y.Username)
.Contains(x.UserName))
.Select(x => x)
.Distinct().ToList();
These two methods are working, but they are not efficient. I tried other ways as well, they are working but not fast. I don't know how to make it more efficient.
Is there any other way to improve the performance of these queries?
Thank you!
Most efficient is to use Window Function ROW_NUMBER, but with EF Core you have to use
FromSqlLess efficient, but With EF Core 6 and higher, should work this query:
If you don't want to leave LINQ and produce most effective SQL, you can use extension linq2db.EntityFrameworkCore (note that I'm one of the creators).
Extension allows using Window Functions in LINQ Query:
Note that it is good to specify how to Order items in
MachineNamegroup. Many databases do not allowROW_NUMBERwithoutORDER BYpart.