EF6 Linq查询以视图模型联接表

时间:2019-03-27 18:52:40

标签: c# asp.net-mvc linq

当前的错误代码给我带来了我无法理解的问题,第67行:

  

LINQ to Entities不支持指定的类型成员'ClinicalAssetID'。仅支持初始化程序,实体成员和实体导航属性。

我试图在DashBoard视图中使用两个具有关系数据的模型,它们是使用ClinicalAssetIDLINQ的{​​{1}}

不确定我的代码是否正确?

控制器:

viewmodel

临床ASSPATVM:

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));

ClinicalAsset:

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; }
    }

}

ClinicalPAT:

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; }
    }
}

1 个答案:

答案 0 :(得分:1)

更改

while(true)
{
    for(int i = 0; i < myObjects_ptrs.size(); ++i)
    {
        myObjects_ptr[i]->doSomething();
        //and here I need to circle back
        for(int j = i + 1; j < myObjects_ptr.size(); ++j)
        {
            //do some things with each other object
        }
        for(int j = 0; j < i; ++j)
        {
            //do the same things with the rest of the objects
        }
    }
}

收件人

if

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