简体   繁体   中英

LINQ to Entities does not recognize the method 'System.String SetAssetStatus(Int32)' method

I'm trying to set a value using a function, inside of a linq select new() block:

            var assetList = DbContext.Assets.Where(x => x.IsActive && x.Parent == null);

            var assets = assetList.Where(x => x.HardwareType_SK != 37 && x.HardwareType_SK != 28 && x.IsActive && x.IsVisible && x.Parent == null).OrderBy(x => x.SortOrder).Select(x => new Models.Asset()
            {
                ID = x.Asset_SK,
                Status = SetAssetStatus(x.Asset_SK),
                Description = x.Description,
                AssociatedCNEStatus = "fmc",
                ChildStatus = "fmc",
                NetworkStatus = "",
                Abbreviation = x.Name,
                Number = x.SortOrder.ToString(),
                Name = x.Name,
                Enviroment = "dev",
                IsEmpty = x.AssetType_SK == 8,
                SiteID = x.Site_SK,
                HardwareDescription = x.HardwareType.Description,
                HardwareType_SK = x.HardwareType_SK
            });

It is failing here:

Status = SetAssetStatus(x.Asset_SK),

My method:

        public string SetAssetStatus(int assetID)
        {
            //var assetIntID = int.Parse(assetID);
            var problemReportStatuses = DbContext.ProblemReportDetails.Where(x => x.ProblemReport.Asset_SK == assetID).OrderByDescending(x => x.MaintenanceStatus_SK).Select(x => x.MaintenanceStatus_SK).ToList();
            if (problemReportStatuses.Min() == 1)
            {
                return "fmc";
            }
            else if(problemReportStatuses.Min() == 2)
            {
                return "pmc";
            }
            else
            {
                return "nmc";
            }
        }

I get the following:

LINQ to Entities does not recognize the method 'System.String SetAssetStatus(Int32)' method, and this method cannot be translated into a store expression

Am I not allowed to pass the database value (x.Asset_SK) into my function to use?

I've tried using int.Parse(X.Asset_SK) and x.Asset_SK.ToString() but same error..

A workaround I finally found online is using AsEnumerable() in my LINQ query, just before the.Select():

var assets = assetList.Where(x => x.HardwareType_SK != 37 && x.HardwareType_SK != 28 && x.IsActive && x.IsVisible && x.Parent == null && x.Asset_SK == 49).OrderBy(x => x.SortOrder).AsEnumerable().Select(x => new Models.Asset()

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.

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