Reputation: 123
I have 700 records that have different columns with type string and I want to select all records using this code:
public virtual object GetAll()
{
var AllList = unitOfWork.Repository<STUser>().GetAll().ToList();
return (from STUser in AllList
select new
{
STUser.ID,
FullName = STUser.HRPerson.LastName,
STUser.USR,
STUser.Active,
STUser.TryCount,
STUser.Description
}).OrderByDescending(i => i.ID).ToList();
}
However, unitl Fill "var AllList" is good but when Linq query execution is very slow.
I removed FullName = STUser.HRPerson.LastName,
line and it was executed very fast.
Join
clause slowed it down.
I ran SQL profiler for every row in the List and it looks like it executes the Query 700 times. Query is
exec sp_executesql N'SELECT
[Extent1].[ID] AS [ID],
[Extent1].[FirstName] AS [FirstName],
[Extent1].[LastName] AS [LastName],
[Extent1].[EnFullName] AS [EnFullName],
[Extent1].[FatherName] AS [FatherName],
[Extent1].[GenderID] AS [GenderID],
[Extent1].[NationalCode] AS [NationalCode],
[Extent1].[IdentityNumber] AS [IdentityNumber],
[Extent1].[BirthCityID] AS [BirthCityID],
[Extent1].[BirthDate] AS [BirthDate],
[Extent1].[VeteranID] AS [VeteranID],
[Extent1].[PersonPic] AS [PersonPic],
[Extent1].[Active] AS [Active],
[Extent1].[Description] AS [Description],
[Extent1].[DateTimes] AS [DateTimes]
FROM [dbo].[HRPerson] AS [Extent1]
WHERE [Extent1].[ID] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=1502
Please help me.
Upvotes: 1
Views: 1981
Reputation: 123
i use IQueryable in repo and add Include(u => u.HRPerson) its ok
var AllList = unitOfWork.Repository<STUser>().GetAll().Include(u => u.HRPerson).ToList();
thankyou @HansKesting for your help
Upvotes: 1
Reputation: 13
You could try using .AsNoTracking() on your query.
From the Microsoft docs -
No tracking queries are useful when the results are used in a read-only scenario. They're quicker to execute because there's no need to set up the change tracking information. If you don't need to update the entities retrieved from the database, then a no-tracking query should be used. You can swap an individual query to be no-tracking.
More here - https://learn.microsoft.com/en-us/ef/core/querying/tracking
Upvotes: 0
Reputation: 22038
Just a thought I would like to share: (I'm not familiar with that platform)
What about this:
public virtual IEnumerable<YourDataObject> GetAll()
{
var result = from STUser in unitOfWork.Repository<STUser>().GetAll() // << what is GetAll()??
orderby STUser.ID
select new YourDataObject // <-- you should create a data holder class for it
{
STUser.ID,
FullName = STUser.HRPerson.LastName,
STUser.USR,
STUser.Active,
STUser.TryCount,
STUser.Description
});
return result;
}
By doing the ordering before the creation of the data holder class, you might use an optimalization on the database side.
Upvotes: 0