LINQ to Entities不支持指定的类型成员。 - Linq。包含

时间:2017-05-26 17:35:38

标签: c# entity-framework linq

我正在我的桌子上执行搜索,但是我在搜索的Linq字段在数据库表中找不到,但是我在模型中创建了一个。在我正在搜索的表中,我记录了一个实际上是用户ID的Created_By字段。我显示了我从另一个表构建的用户的全名。我希望用户能够搜索CreatedFullName。在他们正在搜索的表中,结果集显示了我在其模型中定义的CreatedFullName,但它不允许我搜索它,因为它不在实际的表中。我收到以下错误

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

我的模型将字段定义为 -

public string CreateFullName { get; set; }

我将字段添加到

的结果集中
foreach (DeviceLog x in devicelogs)
        {
            //Retreive full name for Created By and Modified By
            x.CreateFullName = GetFullName(x.CREATED_BY);
            if (x.MODIFIED_BY != null)
            {
                x.ModifiedFullName = GetFullName(x.MODIFIED_BY);
            }
         }

我的Linq查询.Where声明

if (!String.IsNullOrEmpty(searchString3))
        {
            devicelogs = devicelogs.Where(s => s.CreateFullName.Contains(searchString3));
            ViewBag.Search = "Search";
        }

我的整个模特

    public int DeviceLogID { get; set; }
    public int DeviceLogTypeID { get; set; }
    public int DeviceID { get; set; }
    [Required]
    [DataType(DataType.MultilineText)]
    [Display(Name = "Device Log Entry")]
    public string DeviceLogEntry { get; set; }
    [Required]
    [Display(Name = "Entry Date")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public System.DateTime EntryDate { get; set; }
    [Display(Name = "Date Created")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public System.DateTime DATE_CREATED { get; set; }
    [Display(Name = "Created By")]
    public string CREATED_BY { get; set; }
    [Display(Name = "Date Modified")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime? DATE_MODIFIED { get; set; }
    [Display(Name = "Modified By")]
    public string MODIFIED_BY { get; set; }
    [Display(Name = "Entry Number")]
    public int EntryNumber { get; set; }
    public bool Corrected { get; set; }

    public virtual Device Device { get; set; }
    public virtual DeviceLogType DeviceLogType { get; set; }
    public virtual ICollection<DeviceLogAudit> DeviceLogAudits { get; set; }

    public string CreateFullName { get; set; }
    public string ModifiedFullName { get; set; }

2 个答案:

答案 0 :(得分:0)

EF无法将您的查询转换为SQL,因为CreateFullName在数据库中不存在。

假设您已经为CREATE_BY和MODIFIED_BY创建了用户表的外键,那么EF应该创建名称为“CREATED_BYUser&#34;”的伪字段。使用您查询中的那些:

devicelogs = devicelogs.Where(s => s.CREATED_BYUser.FullName.Contains(searchString3));

答案 1 :(得分:0)

什么是devicelogs? IQueryable的?首次将数据分配给devicelog时,请尝试使用link_to