【发布时间】:2017-05-26 17:35:37
【问题描述】:
我正在对我的表执行搜索,但是我正在搜索的 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; }
【问题讨论】:
-
有什么问题?如果属性没有对应的列,您希望 EF 如何搜索表?
-
检查答案here,另外,有人应该将其标记为重复
-
你能展示你的模型吗,所有这些,这个
public string CreateFullname {get;set;}没有帮助。它不在数据库中?那么你是如何假设数据库来搜索它的呢?你是先使用代码还是先使用数据库? -
我已经用我的模型编辑了我的原始帖子 --
CreateFullName和ModifiedFullName不是我的数据库表的一部分
标签: c# entity-framework linq