溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

EFCore中的導(dǎo)航屬性是什么

發(fā)布時(shí)間:2021-03-11 15:09:44 來(lái)源:億速云 閱讀:168 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)EFCore中的導(dǎo)航屬性是什么,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話(huà)不多說(shuō),跟著小編一起來(lái)看看吧。

    1 單獨(dú)使用Include

  在介紹這個(gè)方法之前,我來(lái)先貼出實(shí)體之間的關(guān)聯(lián)關(guān)系,假設(shè)這里有三個(gè)相互關(guān)聯(lián)的實(shí)體VehicleWarranty、WarrantyWarningLevel、VehicleWarrantyRepairHistory這三個(gè)實(shí)體后面兩個(gè)都是第一個(gè)的子級(jí)并且并且VehicleWarranty、WarrantyWarningLevel之間的關(guān)系是1對(duì)1的關(guān)系,VehicleWarranty和VehicleWarrantyRepairHistory之間是1:N的關(guān)系,即1對(duì)多的關(guān)系,我們這里貼出具體的Model,從而方便后面分析具體的代碼。

    /// <summary>
    /// 車(chē)輛三包信息(DCSService)
    /// </summary>
    public class VehicleWarranty : Entity<Guid> {
        public VehicleWarranty() {
            Details = new List<VehicleWarrantyRepairHistory>();
        }
 
        //車(chē)輛售后檔案
        public Guid VehicleSoldId { get; set; }
 
        //VIN
        [Required]
        [MaxLength(EntityDefault.FieldLength_50)]
        public string Vin { get; set; }
 
        //產(chǎn)品分類(lèi)
        public Guid? ProductCategoryId { get; set; }
 
        //產(chǎn)品分類(lèi)編號(hào)
        [MaxLength(EntityDefault.FieldLength_50)]
        public string ProductCategoryCode { get; set; }
 
        //產(chǎn)品分類(lèi)名稱(chēng)
        [MaxLength(EntityDefault.FieldLength_100)]
        public string ProductCategoryName { get; set; }
 
        //車(chē)牌號(hào)
        [MaxLength(EntityDefault.FieldLength_50)]
        public string LicensePlate { get; set; }
 
        //發(fā)動(dòng)機(jī)號(hào)
        [MaxLength(EntityDefault.FieldLength_50)]
        public string EngineCode { get; set; }
 
        //變速箱號(hào)
        [MaxLength(EntityDefault.FieldLength_50)]
        public string TransmissionSn { get; set; }
 
        //開(kāi)發(fā)票日期
        public DateTime InvoiceDate { get; set; }
 
        //行駛里程
        public int Mileage { get; set; }
 
        //是否三包期內(nèi)
        public bool? IsInWarranty { get; set; }
 
        //預(yù)警等級(jí)
        public Guid? WarningLevelId { get; set; }
 
        public WarrantyWarningLevel WarningLevel { get; set; }
 
        //等級(jí)編號(hào)
        [MaxLength(EntityDefault.FieldLength_50)]
        public string LevelCode { get; set; }
 
        //等級(jí)名稱(chēng)
        [MaxLength(EntityDefault.FieldLength_100)]
        public string LevelName { get; set; }
 
        //預(yù)警內(nèi)容
        [MaxLength(EntityDefault.FieldLength_800)]
        public string WarningComment { get; set; }
 
        //累計(jì)維修天數(shù)
        public int TotoalRepairDays { get; set; }
 
        //售出后60天/3000KM內(nèi)嚴(yán)重故障次數(shù)
        public int? FNum { get; set; }
 
        //嚴(yán)重安全性能故障累計(jì)次數(shù)
        public int? GNum { get; set; }
 
        //發(fā)動(dòng)機(jī)總成累計(jì)更換次數(shù)
        public int? HNum { get; set; }
 
        //變速箱總成累計(jì)更換次數(shù)
        public int? INum { get; set; }
 
        //發(fā)動(dòng)機(jī)主要零件最大更換次數(shù)
        public int? JNum { get; set; }
 
        //變速箱主要零件最大更換次數(shù)
        public int? KNum { get; set; }
 
        //同一主要零件最大更換次數(shù)
        public int? LNum { get; set; }
 
        //同一產(chǎn)品質(zhì)量問(wèn)題最大累計(jì)次數(shù)(部件+故障+方位)
        public int? MNum { get; set; }
 
        //同一產(chǎn)品質(zhì)量問(wèn)題最大累計(jì)次數(shù)
        public int? NNum { get; set; }
 
        public List<VehicleWarrantyRepairHistory> Details { get; set; }
    }
 
/// <summary>
    /// 三包預(yù)警等級(jí)(DCS)
    /// </summary>
    public class WarrantyWarningLevel : Entity<Guid> {
        //等級(jí)編號(hào)
        [Required]
        [MaxLength(EntityDefault.FieldLength_50)]
        public string Code { get; set; }
 
        //等級(jí)名稱(chēng)
        [Required]
        [MaxLength(EntityDefault.FieldLength_100)]
        public string Name { get; set; }
 
        //顏色
        [Required]
        [MaxLength(EntityDefault.FieldLength_50)]
        public string Color { get; set; }
 
        //備注
        [MaxLength(EntityDefault.FieldLength_200)]
        public string Remark { get; set; }
    }
 
 
    /// <summary>
    /// 車(chē)輛三包信息維修履歷(DCSService)
    /// </summary>
    public class VehicleWarrantyRepairHistory : Entity<Guid> {
        //車(chē)輛三包信息
        [Required]
        public Guid VehicleWarrantyId { get; set; }
 
        public VehicleWarranty VehicleWarranty { get; set; }
 
        //VIN
        [Required]
        [MaxLength(EntityDefault.FieldLength_50)]
        public string Vin { get; set; }
 
        //維修合同
        public Guid RepairContractId { get; set; }
 
        //維修合同編號(hào)
        [Required]
        [MaxLength(EntityDefault.FieldLength_50)]
        public string RepairContractCode { get; set; }
 
        //處理時(shí)間
        public DateTime? DealTime { get; set; }
 
        //經(jīng)銷(xiāo)商
        public Guid DealerId { get; set; }
 
        //經(jīng)銷(xiāo)商編號(hào)
        [Required]
        [MaxLength(EntityDefault.FieldLength_50)]
        public string DealerCode { get; set; }
 
        //經(jīng)銷(xiāo)商名稱(chēng)
        [Required]
        [MaxLength(EntityDefault.FieldLength_100)]
        public string DealerName { get; set; }
 
        //履歷來(lái)源
        public VehicleWarrantyRepairHistorySource Source { get; set; }
 
        //累計(jì)維修天數(shù)
        public int? TotoalRepairDays { get; set; }
 
        //售出后60天/3000KM內(nèi)嚴(yán)重故障次數(shù)
        public int? FNum { get; set; }
 
        //嚴(yán)重安全性能故障累計(jì)次數(shù)
        public int? GNum { get; set; }
 
        //發(fā)動(dòng)機(jī)總成累計(jì)更換次數(shù)
        public int? HNum { get; set; }
 
        //變速箱總成累計(jì)更換次數(shù)
        public int? INum { get; set; }
 
        //發(fā)動(dòng)機(jī)主要零件最大更換次數(shù)
        public int? JNum { get; set; }
 
        //變速箱主要零件最大更換次數(shù)
        public int? KNum { get; set; }
 
        //同一主要零件最大更換次數(shù)
        public int? LNum { get; set; }
 
        //同一產(chǎn)品質(zhì)量問(wèn)題最大累計(jì)次數(shù)(部件+故障+方位)
        public int? MNum { get; set; }
 
        //同一產(chǎn)品質(zhì)量問(wèn)題最大累計(jì)次數(shù)
        public int? NNum { get; set; }
    }

  這里我們貼出第一個(gè)簡(jiǎn)單的查詢(xún)示例,通過(guò)Include方法來(lái)一下子查詢(xún)出關(guān)聯(lián)的三包預(yù)警等級(jí)這個(gè)實(shí)體,在我們的例子中我們返回的結(jié)果是帶分頁(yè)的,而且會(huì)根據(jù)前端傳遞的Dto來(lái)進(jìn)行過(guò)濾,這里我們來(lái)看這段代碼怎么實(shí)體。

/// <summary>
        /// 查詢(xún)車(chē)輛三包信息
        /// </summary>
        /// <param name="input">查詢(xún)輸入</param>
        /// <param name="pageRequest">分頁(yè)請(qǐng)求</param>
        /// <returns>帶分頁(yè)的三包預(yù)警車(chē)輛信息</returns>
        public async Task<Page<GetVehicleWarrantiesOutput>> GetVehicleWarrantiesAsync(GetVehicleWarrantiesInput input, PageRequest pageRequest) {           
            var queryResults = _vehicleWarrantyRepository.GetAll()
                .Include(v => v.WarningLevel)
                .Where(v => _vehicleSoldRepository.GetAll().Any(vs => vs.Status == VehicleStatus.實(shí)銷(xiāo)完成 && v.Vin == vs.Vin));
            var totalCount = await queryResults.CountAsync();
            var pagedResults = await queryResults.ProjectTo<GetVehicleWarrantiesOutput>(_autoMapper.ConfigurationProvider).PageAndOrderBy(pageRequest).ToListAsync();
            return new Page<GetVehicleWarrantiesOutput>(pageRequest, totalCount, pagedResults);
        }

  在這里我們看到了通過(guò)一個(gè)Include就能夠查詢(xún)出關(guān)聯(lián)的實(shí)體,為什么能夠?qū)崿F(xiàn),那是因?yàn)樵赩ehicleWarranty實(shí)體中存在WarrantyWarningLevel實(shí)體的外鍵,并且這里還增加了外鍵關(guān)聯(lián)的實(shí)體,這樣才能夠正確使用InClude方法,并且這個(gè)InClude方法只能夠以實(shí)體作為參數(shù),不能以外鍵作為參數(shù),到了這里我想提出一個(gè)問(wèn)題,這里最終生成的SQL(SqlServer數(shù)據(jù)庫(kù))是left join 還是inner join呢?在看完后面的分析之前需要思考一下。

select top (20)
  [v].[EngineCode],
  [v].[GNum],
  [v].[Id],
  [v.WarningLevel].[Color] as [LevelColor],
  [v].[LevelName],
  [v].[LicensePlate],
  [v].[ProductCategoryName],
  [v].[TotoalRepairDays],
  [v].[Vin]
from [VehicleWarranty] as [v]
  left join [WarrantyWarningLevel] as [v.WarningLevel] on [v].[WarningLevelId] = [v.WarningLevel].[Id]
where EXISTS(
    select 1
    from [VehicleSold] as [vs]
    where ([vs].[Status] = 7) and ([v].[Vin] = [vs].[Vin]))
order by [v].[Vin]

  這里我們看到生成的SQL語(yǔ)句是left join ,那么這里為什么不是inner join呢?這里先給你看具體的答案吧?這里你看懂了嗎?問(wèn)題就處在我這里建立的外鍵是可為空的 public Guid? WarningLevelId { get; set; }、如果是不可為空的外鍵那么生成的SQL就是inner join這個(gè)你可以親自嘗試。另外有一個(gè)需要提醒的就是,如果你像上面的實(shí)體中建立了VehicleWarranty、WarrantyWarningLeve之間的關(guān)系的話(huà),遷移到數(shù)據(jù)庫(kù)會(huì)默認(rèn)生成外鍵約束,這個(gè)在使用的時(shí)候需要特別注意,但是如果你只是添加了外鍵而沒(méi)有添加對(duì)應(yīng)的外鍵同名的實(shí)體是不會(huì)生成外鍵約束關(guān)系的,這個(gè)暫時(shí)不理解里面的實(shí)現(xiàn)機(jī)制。

  2 主清單使用Include

  剛才介紹的是1對(duì)1的關(guān)聯(lián)關(guān)系,那么像VehicleWarranty、VehicleWarrantyRepairHistory之間有明顯的主清單關(guān)系,即一個(gè)VehicleWarranty對(duì)應(yīng)多個(gè)VehicleWarrantyRepairHistory的時(shí)候使用InClude方法會(huì)生成什么樣的SQL語(yǔ)句呢?這里我也貼出代碼,然后再來(lái)分析生成的SQL語(yǔ)句。

/// <summary>
        /// 查詢(xún)特定的三包預(yù)警車(chē)輛信息
        /// </summary>
        /// <param name="id">特定Id</param>
        /// <returns>特定的三包預(yù)警車(chē)輛信息</returns>
        public async Task<GetVehicleWarrantyWithDetailsOutput> GetVehicleWarrantyWithDetailsAsync(Guid id) {
            var query = await _vehicleWarrantyRepository.GetAll()
                .Include(v => v.WarningLevel)
                .Include(v => v.Details)
                .SingleOrDefaultAsync(v => v.Id == id);
            if (null == query) {
                throw new ValidationException("找不到當(dāng)前特定的三包預(yù)警車(chē)輛信息");
            }
 
            var retResult = ObjectMapper.Map<GetVehicleWarrantyWithDetailsOutput>(query);
            return retResult;
        }

  這里使用了兩個(gè)InClude方法,那么EFCore會(huì)怎么生成這個(gè)SQL呢?通過(guò)查詢(xún)最終的SQL我們發(fā)現(xiàn)EFCore在處理這類(lèi)問(wèn)題的時(shí)候是分開(kāi)進(jìn)行查詢(xún),然后再合并到查詢(xún)的實(shí)體中去的,所以在這個(gè)查詢(xún)的過(guò)程中生成的SQL如下:

select top (2)
  [v].[Id],
  [v].[EngineCode],
  [v].[FNum],
  [v].[GNum],
  [v].[HNum],
  [v].[INum],
  [v].[InvoiceDate],
  [v].[IsInWarranty],
  [v].[JNum],
  [v].[KNum],
  [v].[LNum],
  [v].[LevelCode],
  [v].[LevelName],
  [v].[LicensePlate],
  [v].[MNum],
  [v].[Mileage],
  [v].[NNum],
  [v].[ProductCategoryCode],
  [v].[ProductCategoryId],
  [v].[ProductCategoryName],
  [v].[TotoalRepairDays],
  [v].[TransmissionSn],
  [v].[VehicleSoldId],
  [v].[Vin],
  [v].[WarningComment],
  [v].[WarningLevelId],
  [v.WarningLevel].[Id],
  [v.WarningLevel].[Code],
  [v.WarningLevel].[Color],
  [v.WarningLevel].[Name],
  [v.WarningLevel].[Remark]
from [VehicleWarranty] as [v]
  left join [WarrantyWarningLevel] as [v.WarningLevel] on [v].[WarningLevelId] = [v.WarningLevel].[Id]
where [v].[Id] = @__id_0
order by [v].[Id]
 
select
  [v.Details].[Id],
  [v.Details].[DealTime],
  [v.Details].[DealerCode],
  [v.Details].[DealerId],
  [v.Details].[DealerName],
  [v.Details].[FNum],
  [v.Details].[GNum],
  [v.Details].[HNum],
  [v.Details].[INum],
  [v.Details].[JNum],
  [v.Details].[KNum],
  [v.Details].[LNum],
  [v.Details].[MNum],
  [v.Details].[NNum],
  [v.Details].[RepairContractCode],
  [v.Details].[RepairContractId],
  [v.Details].[Source],
  [v.Details].[TotoalRepairDays],
  [v.Details].[VehicleWarrantyId],
  [v.Details].[Vin]
from [VehicleWarrantyRepairHistory] as [v.Details]
  inner join (
               select distinct [t].*
               from (
                      select top (1) [v0].[Id]
                      from [VehicleWarranty] as [v0]
                        left join [WarrantyWarningLevel] as [v.WarningLevel0]
                          on [v0].[WarningLevelId] = [v.WarningLevel0].[Id]
                      where [v0].[Id] = @__id_0
                      order by [v0].[Id]
                    ) as [t]
             ) as [t0] on [v.Details].[VehicleWarrantyId] = [t0].[Id]
order by [t0].[Id]

  這個(gè)在查詢(xún)的過(guò)程中會(huì)分作幾個(gè)SQL查詢(xún)并且會(huì)將前面查詢(xún)的結(jié)果作為后面查詢(xún)的部分條件來(lái)進(jìn)行了,待整個(gè)查詢(xún)完畢后再在內(nèi)存中將這些結(jié)果組合到一個(gè)query對(duì)象中。

  3 ThenInclude用法

  上面的介紹完了之后,你應(yīng)該能夠明白這個(gè)Include的具體含義和用法了,接著上面的例子,如果WarrantyWarningLevel里面還有通過(guò)外鍵Id去關(guān)聯(lián)別的實(shí)體,這個(gè)時(shí)候ThenInclude就派上了用場(chǎng)了,理論上只要彼此之間建立了這種外鍵關(guān)系就可以一直ThenInClude下去,但是一般情況下不會(huì)用到這么復(fù)雜的情況,當(dāng)然這里面每一個(gè)Include也都是作為一個(gè)單獨(dú)的查詢(xún)來(lái)進(jìn)行的,這個(gè)也可以找具體的例子進(jìn)行試驗(yàn),這里也貼出一個(gè)具體的例子吧。

public async Task<GetRepairContractDetailForSettlementOutput> GetById(Guid id) {
            var repairContract = await _repairContractRepository.GetAll()
                .Include(d => d.RepairContractWorkItems)
                    .ThenInclude(w => w.Materials)
                .FirstOrDefaultAsync(r => r.Id == id);
 
            if (repairContract == null)
                throw new ValidationException(_localizer["當(dāng)前維修合同不存在"]);
            var vehicleSold = _vehicleSoldRepository.Get(repairContract.VehicleId);
            var isTrafficSubsidy = _repairContractManager.IsTrafficSubsidy(repairContract.Id);
            var (nextMaintenanceMileage, nextMaintenanceTime) = _repairContractManager.GetNextMaintainInfo(repairContract, vehicleSold);
            var result = new GetRepairContractDetailForSettlementOutput() {
                Id = repairContract.Id,
                Code = repairContract.Code,
                CustomerName = repairContract.CustomerName,
                CellPhoneNumber = repairContract.CellPhoneNumber,
                Vin = repairContract.Vin,
                LicensePlate = repairContract.LicensePlate,
                NextMaintenanceTime = nextMaintenanceTime,
                NextMaintenanceMileage = nextMaintenanceMileage,
                LaborFee = repairContract.LaborFee,
                LaborFeeAfter = repairContract.LaborFeeAfter,
                MaterialFee = repairContract.MaterialFee,
                MaterialFeeAfter = repairContract.MaterialFeeAfter,
                OutFee = repairContract.OutFee,
                OtherFee = repairContract.OtherFee,
                TotalFeeAfter = repairContract.TotalFeeAfter,
                ShowIsTrafficSubsidy = isTrafficSubsidy,
                LastMaintenanceTime = vehicleSold.LastMaintenanceTime,
                LastMaintenanceMileage = vehicleSold.LastMaintenanceMileage,
                WorkItems = _mapper.Map<IList<GetRepairContractWorkItemForSettlementOutput>>(repairContract.RepairContractWorkItems)
            };
            return result;
        }

  最后還要介紹一種極特殊的情況,由于ThenInclude方法只能一層層向下進(jìn)行,如果我想對(duì)同一個(gè)實(shí)體里面的兩個(gè)關(guān)聯(lián)實(shí)體做ThenInclude操作這個(gè)怎么處理,這里就直接給出代碼吧。

/// <summary>
        ///維修合同完成的事件
        /// </summary>
        /// <param name="repairContractId"></param>
        public void Finished(Guid repairContractId) {
            var repairContract = _repairContractRepository.GetAll()
                .Include(c => c.RepairContractWorkItems).ThenInclude(wi => wi.Materials)
                .Include(c => c.RepairContractWorkItems).ThenInclude(wi => wi.Fault)
                .SingleOrDefault(c => c.Id == repairContractId);
            var repairContractAdjusts = _repairContractAdjustRepository.GetAll()
                .Include(a => a.WorkItems).ThenInclude(w => w.Materials)
                .Where(a => a.RepairContractId == repairContractId).ToList();
 
            var @event = new AddRepairContractEvent {
                Key = repairContract?.Code,
                RepairContract = repairContract,
                RepairContractAdjusts = repairContractAdjusts
            };
            _producer.Produce(@event);
        }

  這里需要Include同一個(gè)實(shí)體兩次,然后分別調(diào)用ThenInclude方法,這個(gè)屬于比較特殊的情況,在使用的時(shí)候需要注意。

  溫馨提示:

  這里讀者在看代碼的時(shí)候可能不太理解類(lèi)似這種 _repairContractRepository的具體由來(lái),這里貼出一份完整的代碼。

internal class AddRepairContractEventManager : DomainService, IAddRepairContractEventManager {
        private readonly KafkaProducer _producer;
        private readonly IRepository<RepairContract, Guid> _repairContractRepository;
        private readonly IRepository<RepairContractAdjust, Guid> _repairContractAdjustRepository;
 
        public AddRepairContractEventManager(KafkaProducer producer,
                                          IRepository<RepairContract, Guid> repairContractRepository,
                                          IRepository<RepairContractAdjust, Guid> repairContractAdjustRepository) {
            _producer = producer;
            _repairContractRepository = repairContractRepository;
            _repairContractAdjustRepository = repairContractAdjustRepository;
        }
 
        /// <summary>
        ///維修合同完成的事件
        /// </summary>
        /// <param name="repairContractId"></param>
        public void Finished(Guid repairContractId) {
            var repairContract = _repairContractRepository.GetAll()
                .Include(c => c.RepairContractWorkItems).ThenInclude(wi => wi.Materials)
                .Include(c => c.RepairContractWorkItems).ThenInclude(wi => wi.Fault)
                .SingleOrDefault(c => c.Id == repairContractId);
            var repairContractAdjusts = _repairContractAdjustRepository.GetAll()
                .Include(a => a.WorkItems).ThenInclude(w => w.Materials)
                .Where(a => a.RepairContractId == repairContractId).ToList();
 
            var @event = new AddRepairContractEvent {
                Key = repairContract?.Code,
                RepairContract = repairContract,
                RepairContractAdjusts = repairContractAdjusts
            };
            _producer.Produce(@event);
        }
    }

  4 IncludeFilter用法

  在有些場(chǎng)景中我們可能需要帶出清單的時(shí)候并且過(guò)濾清單,這個(gè)功能算是對(duì)Include方法的一個(gè)提升,可以將兩個(gè)操作合并到一起來(lái)進(jìn)行,這個(gè)在使用的時(shí)候需要注意 這個(gè)并不是Asp.Net Core自帶的功能,這個(gè)需要通過(guò)引入包 Z.EntityFramework.Plus.EFCore.dll的包來(lái)實(shí)現(xiàn)的,如果你們的系統(tǒng)中使用的是ABP作為項(xiàng)目主框架,那么你只需要引用 Abp.EntityFrameworkCore.EFPlus這個(gè)包就可以了,因?yàn)檫@個(gè)包中就包含和Z.EntityFramework相關(guān)的子包,這個(gè)在使用的時(shí)候需要注意。

   下面我們來(lái)看一看我們的代碼中是怎么使用的。

private (Company company, IEnumerable<PartSaleOrderType> partSaleOrderTypes) GetCompanyDetailForFactory(Guid id) {
            var currentPartSaleOrderTypes = GetCurrentPartSaleOrderTypes();
            var currentPartSaleOrderTypeIds = currentPartSaleOrderTypes.Select(t => t.Id);
            var company = _companyRepository.GetAll()
                .IncludeFilter(c => c.CustomerPartInformations.Where(i => i.BranchId == SdtSession.TenantId.GetValueOrDefault()))
                .IncludeFilter(c => c.CustomerOrderWarehouses.Where(w => currentPartSaleOrderTypeIds.Contains(w.PartSaleOrderTypeId)))
                .IncludeFilter(c => c.CustomerMarkupRates.Where(w => currentPartSaleOrderTypeIds.Contains(w.PartSaleOrderTypeId)))
                .IncludeFilter(c => c.OrderShippingSequences.Where(w => currentPartSaleOrderTypeIds.Contains(w.PartSaleOrderTypeId)))
                .IncludeFilter(c => c.OrderingCalendars.Where(w => currentPartSaleOrderTypeIds.Contains(w.PartSaleOrderTypeId)))
                .FirstOrDefault(d => d.Id == id && d.Status == BaseDataStatus.生效);
            if (company == null) {
                throw new EntityNotFoundException(SharedLocalizer["未能找到對(duì)應(yīng)的企業(yè)"]);
            }
            return (company, currentPartSaleOrderTypes);
        }

  這個(gè)提供了一種新的清單過(guò)濾方式,不僅提高了效率而且使代碼更加優(yōu)化簡(jiǎn)練。

  5 特殊情況

  這里還介紹一種不通過(guò)Include方法來(lái)獲取清單中的方式,就像下面這種寫(xiě)法,Company對(duì)象和OrderingCalendars之間建立了一對(duì)多的導(dǎo)航屬性,我們?cè)谑褂?nbsp;company.OrderingCalendars之前先將內(nèi)部的清查查詢(xún)出來(lái)(不需要定義變量接收,使用_即可),這樣也是能通過(guò)導(dǎo)航屬性自動(dòng)映射到company的OrderingCalendars中去的,這個(gè)在使用的時(shí)候需要特別注意,查詢(xún)的時(shí)候要提前ToList,將數(shù)據(jù)查詢(xún)到內(nèi)存里面

_ = _orderingCalendarRepository.GetAll().Where(t => t.OrderingCompanyId == company.Id).ToList();           
           var tempCalendars = company.OrderingCalendars.OrderBy(d => d.PartSaleOrderType.Level).Select(d => new {
               d.PartSaleOrderType.BrandId,
               d.PartSaleOrderType.BrandCode,
               d.PartSaleOrderType.BrandName,
               d.PartSaleOrderTypeId,
               d.PartSaleOrderTypeCode,
               d.PartSaleOrderTypeName,
               d.Year,
               d.Month,
               d.Day
           });

以上就是EFCore中的導(dǎo)航屬性是什么,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI