2017-05-26 25 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."}

として私のモデルは、フィールドを定義します - 私は

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によって結果セットにフィールドを追加

public string CreateFullName { get; set; }

ステートメント

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

CreateFullNameがデータベースに存在しないので、私の全体のモデル

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

あなたのモデル、そのすべてを表示できますか?この 'public string CreateFullname {get; set;}'は役に立ちません。データベースにはありませんか?どのようにデータベースを検索すると仮定していますか? 最初にコードまたはデータベースを使用していますか? – Munzer

答えて

0

最初にデータをデバイスに割り当てるときに.ToList();を使用してください

+0

私は私のlinqクエリに '.ToList();を追加する前に' List'を作成しましたそれを動作させるには - 'List devicelogs = logs.ToList();'と 'devicelogs = devicelogs.Where(s => s.CreateFullName.Contains(searchString3))。ToList();' –

0

EFは、SQLにクエリを変換することはできません。

は、EFのshouldhaveは `CREATED_BYUser」のような名前の擬似フィールドを作成し、あなたはUsersテーブルにCREATED_BYとMODIFIED_BYの外部キーを作成したと仮定あなたの中にそれらを使用して照会:。

devicelogsは何
devicelogs = devicelogs.Where(s => s.CREATED_BYUser.FullName.Contains(searchString3)); 
関連する問題