[英]EF Core string contains string array
Is it possible to check with EF Core if a string contains a string[]是否可以使用 EF Core 检查字符串是否包含字符串 []
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.无法翻译 LINQ 表达式 'DbSet().Where(g => __keywords_0.Any(j => g.Keywords.Contains(j)))'。
So how wil I rewrite this so that it can be translated to SQL?那么我将如何重写它以便可以将其翻译为 SQL? It is not an option to get a complete list and checking it with C#.获取完整列表并使用 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>
List<StringList>
将替换List<String>
For Example DB.Products.Include(x => x.Strings).ToList().ForEach(y => Console.WriteLine(y.Name));
例如DB.Products.Include(x => x.Strings).ToList().ForEach(y => Console.WriteLine(y.Name));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.