Jay
Jay

Reputation: 1033

LINQ query a list inside a record

EDIT Turns out EF wont map List strings to a table. So to fix it I've created a simple table with an id, a string field and a foreign Key to the Example table, then in the example table i've updated the List<> attribute to point to this table. ...I think I had a brain fart. sorry all.

I've tried the other questions but I'm still getting the same error.

I have a table with the following:

 public class Example
{
   public Example()
   {
       ExampleList = new List<string>();         
   }

   public int Id { get; set; }
   public string Name { get; set; }
   public string Description { get; set; }      
   public List<string> ExampleList{ get; set; }
}

Now when I go to query the database, I want to return back entries that contain "imastring" in their ExampleList I'm currently using:

    var test = db.Examples.Where(c => c.ExampleList.Count(z => z.Contains("imastring")) == 0).ToList();

However, this query brings back the following error:

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

I can't figure out whats wrong, what am i missing guys?

For clarity

I'm trying to bring back all "Example" record whos "ExampleList<>" contains the string "imastring"

Upvotes: 1

Views: 324

Answers (1)

Rafał Straszewski
Rafał Straszewski

Reputation: 998

You cannot store in database list of string type (or primitives). You need to create for example ExampleTable class with id and string value. Or keep whole list in one property (for example strings separated with ';') and parse given string with split. Or serialize list to json and save as string property.

Upvotes: 1

Related Questions