I'm currently working on an ASP.NET Mvc 4.5 application and use a basic Repository Pattern. In my service implementation I'm using a GetById method, which works fine, but is unfortunately kind of slow.
I'm selecting into a ViewModel from different tables, though.
In the controller I'm calling:
var offerVm = OfferService.GetOfferById(Id);
The service class implements the method as follows:
public OfferVm GetOfferById(int id)
{
var offer = Db.Offer
.Where(x => x.OfferId == id)
.Select(x => new OfferVm
{
OfferId = x.OfferId,
CreatedById = x.CreatedById,
CreatedDate = x.CreatedDate,
Label = x.OfferData.Label,
StatusId = x.OfferData.StatusId,
HasPresentationDocument = x.OfferData.HasPresentationDocument,
Actors = x.ActorPool.Actor.Select(y => new ActorVm
{
EmployeeId = y.EmployeeId,
ActorTypeId = y.ActorTypeId
}).ToList(),
Targets = x.TargetPool.Target.Select(y => new TargetVm
{
IsLargeCompany = y.IsLargeCompany,
IsSmallCompany = y.IsSmallCompany,
IsPrivateCompany = y.IsPrivateCompany,
IsPublicCompany = y.IsPublicCompany,
CountryIds = y.TargetScope.Select(z => z.CountryId).ToList()
}).ToList(),
Swots = x.SwotPool.SWOT.Select(y => new SwotVm
{
SwotForId = y.SwotForId,
SwotParts = y.SwotParts.Select(z => new SwotPartVm
{
SwotTypeId = z.SwotTypeId,
Label = z.Label
})
}).ToList()
}).FirstOrDefault();
}
Do you have some ideas/inspirations regarding Linq and on how to speed up/enhance this query ?
Thanks!!
If you are using Entity Framework, I highly suggest to make a Stored Procedure instead of joining it all via LINQ. There will be a big difference in terms of performance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.