简体   繁体   English

选择EF Core中的性能问题

[英]Select performance problems in EF Core

        _context.Homestays   
                .Include(x => x.CreatedUser)
                .Include(x => x.UpdatedUser)
                .Include(x => x.HomestayEvaluations)
                .Include(x => x.HomestayContracts)
                .Include(x => x.HomestayPoliceChecks)
                .Include(x => x.HomestayHouseHolds)
                .AsNoTracking()
                .Select(x => new Homestay()
                {
                    HomestayId = x.HomestayId,
                    HomestayFamily = ConstValue.GetHomestayFamilyName(x),
                    Address = x.Address,
                    Score = x.HomestayEvaluations.Any(x1 => x1.IsEvaluationActive) ? x.HomestayEvaluations.LastOrDefault(x1 => x1.IsEvaluationActive).GetScore() : 0,
                    Contract = x.HomestayContracts.Any(x1 => x1.IsContractActive) ? x.HomestayContracts.LastOrDefault(x1 => x1.IsContractActive).ContractDate : null,
                    Students = x.Students,
                    HouseHolders = x.HomestayHouseHolds.Count(x1 => x1.IsHouseHoldActive),
                    PoliceCheck = x.HomestayPoliceChecks.Any(x1 => x1.IsPoliceCheckActive) ? x.HomestayPoliceChecks.LastOrDefault(x1 => x1.IsPoliceCheckActive).PoliceCheckDate : null,
                    Language = x.Language,
                    Room = x.Room,
                    IsActive = x.IsActive,
                    CreatedDate = x.CreatedDate,
                    CreatedUserName = ConstValue.GetUserName(x.CreatedUser),
                    UpdatedDate = x.UpdatedDate,
                    UpdatedUserName = ConstValue.GetUserName(x.UpdatedUser)
                })
                .OrderByDescending(x => x.HomestayId);

Hello, Im wonder that how do I change my select query better ? 您好,我想知道如何更好地更改选择查询?

This code as below that it looks messy. 下面的代码看起来很杂乱。 first excute any and if it is true, get last one. 首先执行任何一个,如果是,则执行最后一个。 but is there any more short code ?? 但是还有更多短代码吗?

Contract = x.HomestayContracts.Any(x1 => x1.IsContractActive) ? 合约= x.HomestayContracts.Any(x1 => x1.IsContractActive)吗? x.HomestayContracts.LastOrDefault(x1 => x1.IsContractActive).ContractDate : null x.HomestayContracts.LastOrDefault(x1 => x1.IsContractActive).ContractDate:null

I tried without any(), it occred error as null object if there is no data. 我尝试不使用any(),如果没有数据,它将作为空对象出现错误。

Please help me. 请帮我。

Thanks :) 谢谢 :)

Use this 用这个

Contract = x.HomestayContracts.LastOrDefault(x1 => x1.IsContractActive)?.ContractDate ?? null

Also you can disable lazy loading for this query in context's configuration 您也可以在上下文的配置中禁用此查询的延迟加载

context.Configuration.LazyLoadingEnabled = false; 

This way you can eliminate Include methods, cause shorter statement but not better performance. 这样,您可以消除Include方法,导致语句更短,但性能不会更好。 Use stored procedure for performance. 使用存储过程来提高性能。 Stored Procedure in Entity Framework 实体框架中的存储过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM