2017-05-26 97 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)); 
相關問題