简体   繁体   中英

EF Core string contains string array

Is it possible to check with EF Core if a string contains a string[]

For example, I have a database table that has comma separated keywords in one columns. I want to get related records that also contain these keywords. The way I tried it:

public class Item {

  int Id { get; set; }

  string Keywords { get; set; }

}
var keywords = item.Keywords.Split(',');

var result = context.Items.Where(i => keywords.Any(j => i.Keywords.Contains(j)).ToListAsync();

This will throw a error message:

The LINQ expression 'DbSet().Where(g => __keywords_0.Any(j => g.Keywords.Contains(j)))' could not be translated.

So how wil I rewrite this so that it can be translated to SQL? It is not an option to get a complete list and checking it with C#. It is also not possible to convert a comma seperated keyword list to it's own table.

You can just use something like this

public class Product
{
    public Int32 ID { get; set; }
    public List<StringList> Strings{ get; set; }

}

public class StringList
{
    public Int32 Id { get; set; }
    public String Name { get; set; }
}

List<StringList> will replace List<String>
For Example DB.Products.Include(x => x.Strings).ToList().ForEach(y => Console.WriteLine(y.Name));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM