简体   繁体   中英

EF6 Linq query for joining tables with view model

The current error code presents me with an issue I can't decipher, Line 67:

The specified type member 'ClinicalAssetID' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

I am trying to bring into the DashBoard view two models that have relational data which is the ClinicalAssetID using LINQ and viewmodel

Not sure if my code is going the right way about it ?

Controller:

namespace Assets.Areas.Clinical.Controllers
{
    public class ClinicalAssetsController : Controller
    {
        private ClinicalContext db = new ClinicalContext();
        [Authorize]
        // GET: Clinical/ClinicalAssets
        public async Task<ActionResult> DashBoard(string sortOrder, string currentFilter,string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var clinicalAssets = (from s in db.ClinicalAssets
                                  join cp in db.ClinicalPATs on s.ClinicalAssetID equals cp.ClinicalAssetID


                                 select new ClinicalASSPATVM
                                 {
                                     InspectionDocumnets = cp.InspectionDocumnets,
                                 });


            if (!String.IsNullOrEmpty(searchString))
            {
                clinicalAssets = clinicalAssets.Where(s => s.SerialNo.Contains(searchString)
                                       || s.PoNo.Contains(searchString));
            }

            switch (sortOrder)
            {
                case "name_desc":
                    clinicalAssets = clinicalAssets.OrderByDescending(s => s.PoNo);
                    break;
                case "Date":
                    clinicalAssets = clinicalAssets.OrderBy(s => s.PurchaseDate);
                    break;
                default:
                    clinicalAssets = clinicalAssets.OrderBy(s => s.ClinicalAssetID);
                    break;
            }
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(await clinicalAssets.ToPagedListAsync(pageNumber, pageSize));

ClinicalASSPATVM:

namespace Assets.Areas.Clinical.Models
{
    public class ClinicalASSPATVM
    {
        public int ClinicalAssetID { get; set; }
        public string SerialNo { get; set; }
        public DateTime? PurchaseDate { get; set; }
        public string PoNo { get; set; }
        public float? Costing { get; set; }
        public string InspectionDocumnets { get; set; }

        public virtual Model ModelName { get; set; }
        public virtual BudgetCode Code { get; set; }
        public virtual Product ProductName { get; set; }
        public virtual AssetType AssetTypeName { get; set; }
        public virtual Manufacturer ManufacturerName { get; set; }
        public virtual Staff StaffName { get; set; }
        public virtual Team TeamName { get; set; }
        public virtual Supplier SupplierName { get; set; }
    }

}

ClinicalAsset:

namespace Assets.Areas.Clinical.Models
{
    public class ClinicalAsset
    {
        [Key]
        public int ClinicalAssetID { get; set; }
        public int AssetTypeID { get; set; }
        public int? ProductID { get; set; }
        public int? ManufacturerID { get; set; }
        public int? ModelID{ get; set; }
        public int? SupplierID { get; set; }
        [StringLength(100, MinimumLength = 2)]
        public string SerialNo { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yy}", ApplyFormatInEditMode = true)]
        public DateTime? PurchaseDate { get; set; }
        [StringLength(100, MinimumLength = 2)]
        public string PoNo { get; set; }
        [DataType(DataType.Currency)]
        [DisplayFormat(DataFormatString = "{0:C0}")]
        public float? Costing { get; set; }
        public int? TeamID { get; set; }
        public int? BudgetCodeID { get; set; }
        public int? StaffID { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yy}", ApplyFormatInEditMode = true)]


        public virtual Model ModelName { get; set; }
        public virtual BudgetCode Code { get; set; }
        public virtual Product ProductName { get; set; }
        public virtual AssetType AssetTypeName { get; set; }
        public virtual Manufacturer ManufacturerName { get; set; }
        public virtual Staff StaffName { get; set; }
        public virtual Team TeamName { get; set; }
        public virtual Supplier SupplierName { get; set; }
        public List<ClinicalPAT> ClinicalPATs { get; set; }
    }
}

ClinicalPAT:

namespace Assets.Areas.Clinical.Models
{
    public class ClinicalPAT
    {
        [Key]
        public int ClinicalPATID { get; set; }
        public int ClinicalAssetID { get; set; }
        public DateTime? WarrantyEndDate { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:ddd/MMMM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? InspectionDate { get; set; }
        public int? InspectionOutcomeID { get; set; }
        [StringLength(100, MinimumLength = 2)]
        public string InspectionDocumnets { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:ddd/MMMM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? InspectionDueDate { get; set; }

        public virtual InspectionOutcome InspectionOutcomeResult { get; set; }
    }
}

Change

from s in db.ClinicalAssets
join cp in db.ClinicalPATs on s.ClinicalAssetID equals cp.ClinicalAssetID
select new ClinicalASSPATVM
{
    InspectionDocumnets = cp.InspectionDocumnets,
}

To

from s in db.ClinicalAssets
join cp in db.ClinicalPATs on s.ClinicalAssetID equals cp.ClinicalAssetID
select new ClinicalASSPATVM
{
    ClinicalAssetID = cp.ClinicalAssetID,
    InspectionDocumnets = cp.InspectionDocumnets
}

Link: The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported

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