1

I am performing a search on my table however the Linq field I am searching on is not found in the database table but one that I created in the Model. In the table I am searching I record a Created_By field that is actually a user ID. I display the user's full name that I build from another table. I would like the user to be able to search on CreatedFullName. In my table that they are searching the result set shows CreatedFullName that I defined in its Model however it will not allow me to search it because it is not in the actual table. I receive the following error

{"The specified type member 'CreateFullName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."}

My Model defines the field as -

public string CreateFullName { get; set; }

I add the field to the result set by

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);
            }
         }

My Linq query .Where statement

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

My entire Model

    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; }
Ethel Patrick
  • 885
  • 7
  • 18
  • 38
  • What's the question? How do you expect EF to search the table if the property has no corresponding column? – Ivan Stoev May 26 '17 at 17:39
  • Check the answer [here](https://stackoverflow.com/questions/19791350/linq-cant-use-string-contains), also, someone should mark this as a duplicate – yorodm May 26 '17 at 17:46
  • Can you show your model, all of it, this `public string CreateFullname {get;set;}` doesn't help. It is not in the database? so how you are assuming the database to search it ? are you using code first or database first ? – Munzer May 26 '17 at 17:49
  • I have edited my original post with my Model -- `CreateFullName` and `ModifiedFullName` are not part of my database table – Ethel Patrick May 26 '17 at 20:33

2 Answers2

0

EF can't convert your query to SQL, because CreateFullName doesn't exist in the database.

Assume you've created foreign keys for CREATED_BY and MODIFIED_BY to the Users table, then EF shouldhave created pseudo-fields with names like `CREATED_BYUser". Use those in you query:

devicelogs = devicelogs.Where(s => s.CREATED_BYUser.FullName.Contains(searchString3));
James Curran
  • 101,701
  • 37
  • 181
  • 258
0

What is devicelogs? iqueryable? Try to use .ToList(); when you first assign data to devicelogs.

Ppp
  • 1,015
  • 9
  • 14
  • I created a `List` before making the assignment and added `.ToList();` on my linq query to make it work - `List devicelogs = logs.ToList();` and `devicelogs = devicelogs.Where(s => s.CreateFullName.Contains(searchString3)).ToList();` – Ethel Patrick May 31 '17 at 17:27