【问题标题】:The specified type member is not supported in LINQ to Entities. - Linq .ContainsLINQ to Entities 不支持指定的类型成员。 - Linq .包含
【发布时间】: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;} 没有帮助。它不在数据库中?那么你是如何假设数据库来搜索它的呢?你是先使用代码还是先使用数据库?
  • 我已经用我的模型编辑了我的原始帖子 -- CreateFullNameModifiedFullName 不是我的数据库表的一部分

标签: c# entity-framework linq


【解决方案1】:

什么是设备日志?可查询?首次将数据分配给设备日志时,请尝试使用.ToList();

【讨论】:

  • 我在分配之前创建了一个 List 并在我的 linq 查询中添加了 .ToList(); 以使其工作 - List&lt;DeviceLog&gt; devicelogs = logs.ToList();devicelogs = devicelogs.Where(s =&gt; s.CreateFullName.Contains(searchString3)).ToList();
【解决方案2】:

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

假设您已经为 Users 表的 CREATED_BY 和 MODIFIED_BY 创建了外键,那么 EF 应该已经创建了名称类似于“CREATED_BYUser”的伪字段。在您的查询中使用它们:

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

【讨论】:

    猜你喜欢
    • 2015-01-27
    • 2012-07-17
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 2018-02-19
    • 2015-07-25
    相关资源
    最近更新 更多