[英].Contains() in .Where() EF Core doesn't work .NET Core 3.1.8
I have the the following code that doesn't work:我有以下代码不起作用:
var bookIds = source.Sids.Split(",");
var hkm = _context.Books
.Include(b => b.Writer)
.Where(b => bookIds.Contains(b.SttmId.toString()))
.ToList();
To clarify the code above a bit more:为了进一步澄清上面的代码:
// kinda same like the code above (just to clarify what source is):
// This doesn't work
var bookIds = "2194".Split(",");
var hkm = _context.Books
.Include(b => b.Writer)
.Where(b => bookIds.Contains(b.SttmId.toString()))
.ToList();
// This works
var bookIds = 2194;
var hkm = _context.Books
.Include(b => b.Writer)
.Where(b => b.SttmId == bookIds))
.ToList();
b.SttmId
is of the type Nullable Long: long?
b.SttmId
的类型为 Nullable Long: long?
I get a strange error:我收到一个奇怪的错误:
---> System.InvalidOperationException: The LINQ expression 'DbSet.Where(s => __BooksIds_0.Contains(b.SttmId.ToString()))' could not be translated.
---> System.InvalidOperationException:无法翻译 LINQ 表达式“DbSet.Where(s => __BooksIds_0.Contains(b.SttmId.ToString()))”。 Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。
Am I doing something wrong or is there something wrong with EF Core?我做错了什么还是 EF Core 有问题?
This should work:这应该有效:
var bookIds = new long[] { 2194 };
var hkm = _context.Books
.Include(b => b.Writer)
.Where(b => bookIds.Any(x => x == b.SttmId))
.ToArray();
And if so all you have to do is arrange an array of longs eg如果是这样,您所要做的就是安排一系列多头,例如
var bookIds = "1 2 3 4".Split().Select(x => long.Parse(x)).ToArray();
I ran similar code but without .Include(b => b.Writer)
(thinks it doesn't matter) and it works.我运行了类似的代码,但没有
.Include(b => b.Writer)
(认为没关系)并且它有效。 May be this depends on database management system or EntityFrameworkCore version?这可能取决于数据库管理系统或 EntityFrameworkCore 版本吗? I use SQL server and EntityFrameworkCore 3.1.1
我使用 SQL 服务器和 EntityFrameworkCore 3.1.1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.