Entity Framework Core的坑,Select后再對導航屬性進行查詢或Select前進行Skip/Take


把asp.net core的項目發布到ubuntu上了,運行的時候出現了如下警告:

warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'where [g.ResBaseInfo].ResType.Equals(Hotel)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Skip(__p_3)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Take(__p_4)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[10106]
The Include operation for navigation '[g].ResBaseInfo' is unnecessary and was ignored because the navigation is not reachable in the final query results. See https://go.microsoft.com/fwlink/?linkid=850303 for more information.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'where [g.ResBaseInfo].ResType.Equals(Hotel)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Count()' could not be translated and will be evaluated locally.

本來打算不理會的,但是仔細看了下,大概的意思是會把所有數據拉到本地再進行查詢處理,如果數據量大了,服務器肯定遭不住,還是分析代碼找到了原因。

錯誤在於先在一個公共方法里select出了基礎數據,然后在Controller中再次根據條件進行where,如果where的是導航屬性中的屬性,就會報上面的警告。修改了代碼,把對導航屬性的where語句放到select前,問題解決。

        public IQueryable<ResBaseInfoView> GetResBaseInfoViewQueryable(Lang lang, ResType resType = 0)
        {
            var jquery = _context.ResBaseInfoMultipleLanguage
                //.Include(g => g.ResBaseInfo)
                //.ThenInclude(g => g.AreaCode)
                .Where(g => !g.ResBaseInfo.IsDelete && g.Language.Equals(lang));

            if (resType != 0)
            {
                jquery = jquery.Where(g => g.ResBaseInfo.ResType == resType);
            }

            var result = jquery
                .Select(rbml => new ResBaseInfoView
                {
                    ID = rbml.ResBaseInfoID,
                    AddAccountID = rbml.ResBaseInfo.AddAccountID,
                    AreaCodeID = rbml.ResBaseInfo.AreaCodeID,
                    IsDelete = rbml.ResBaseInfo.IsDelete,
                    Language = rbml.Language,
                    MLID = rbml.MLID,
                    UpdateTime = rbml.ResBaseInfo.UpdateTime,
                    ImageName = rbml.ResBaseInfo.ImageName,
                    Name = rbml.Name,
                    ResType = rbml.ResBaseInfo.ResType,
                    ResTypeName = rbml.ResBaseInfo.ResType.GetDescription(lang),
                    AreaName = rbml.ResBaseInfo.AreaCode.GetML_Name(lang),
                    Address = rbml.Address,
                    AddTime = rbml.ResBaseInfo.AddTime,
                    Lng = rbml.ResBaseInfo.Lng,
                    Lat = rbml.ResBaseInfo.Lat,
                    ZoomLevel = rbml.ResBaseInfo.ZoomLevel,
                    IsShow = rbml.ResBaseInfo.IsShow,
                    MobilePhone = rbml.MobilePhone,
                    Telephone = rbml.Telephone,
                    Sequence = rbml.ResBaseInfo.Sequence
                });

            return result;
        }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM