2017-05-26 94 views
0

我在我的表上执行搜索,但是我搜索的Linq字段未在数据库表中找到,而是在模型中创建的。在我搜索的表中,我记录了一个实际上是用户ID的Created_By字段。我显示从另一个表格构建的用户的全名。我希望用户能够搜索CreatedFullName。在我的表中,他们正在搜索结果集显示CreatedFullName,我在其模型中定义,但它不会允许我搜索它,因为它不是在实际的表中。我收到以下错误LINQ to Entities不支持指定的类型成员。 - Linq .Contains

{"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; } 
+0

什么问题?你如何期望EF搜索表,如果该属性没有相应的列? –

+0

检查答案[这里](https:/ /stackoverflow.com/questions/19791350/linq-cant-use-string-contains),另外,有人应该将此标记为重复 – yorodm

+0

你可以显示你的模型,所有的这个'公共字符串CreateFullname {get; set;}'没有帮助。它不在数据库中?所以你如何在数据库中进行搜索? 你是先使用代码还是先使用数据库? – Munzer

回答

0

什么是devicelogs ?iqueryable?尝试使用.ToList();当你第一次分配数据devicelogs

+0

我在创建一个'List'之前做了任务并在我的linq查询中添加了'.ToList() ();'列表 devicelogs = logs.ToList();'和'devicelogs = devicelogs.Where(s => s.CreateFullName.Contains(searchString3))。ToList();' –

0

EF不能查询转换为SQL,因为CreateFullName不存在于数据库中。 。

假设你已经创建了CREATED_BY和MODIFIED_BY外键的用户表,然后EF shouldhave创建伪领域与像`CREATED_BYUser”名称使用那些在您查询:

devicelogs = devicelogs.Where(s => s.CREATED_BYUser.FullName.Contains(searchString3)); 
相关问题