简体   繁体   English

转换 IQueryable<t> 列出<t>将 EF 核心与 postgres 数据库一起使用</t></t>

[英]Converting IQueryable<T> to List<T> using EF core with postgres DB

I am using a postgres DB and EF core.我正在使用 postgres 数据库和 EF 核心。 I have the following code get data using DB context我有以下代码使用数据库上下文获取数据

IQueryable<string> projectIds = context.Projects.Where(x => x.Collaborators.Contains(userId)).Select(x => x.Id);
List<string> ids = projectIds.ToList();

When I execute projectIds.ToList() , I get the exception (copied below).当我执行projectIds.ToList()时,出现异常(复制如下)。

What is the right way of using IQueryable?使用 IQueryable 的正确方法是什么? Or is there any other way to convert IQueryable to List?或者还有其他方法可以将 IQueryable 转换为 List 吗?

Any suggestions would help a lot..任何建议都会有很大帮助..

Exception details:异常详情:

  HResult=0x80004005
  Message=42883: operator does not exist: character varying[] @> text[]
  Source=Npgsql
  StackTrace:
   at Npgsql.NpgsqlConnector.<<ReadMessage>g__ReadMessageLong|194_0>d.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at Npgsql.NpgsqlDataReader.<NextResult>d__44.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Npgsql.NpgsqlDataReader.NextResult()
   at Npgsql.NpgsqlCommand.<ExecuteReader>d__100.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Npgsql.NpgsqlCommand.<ExecuteReader>d__100.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)```

Thanks for all the suggestions in comments.感谢评论中的所有建议。 After hours of trial found out that the error was not during converting to ToList().经过几个小时的试验发现错误不是在转换为 ToList() 期间发生的。

The error lies with using contains operator on Character varying[]错误在于在Character varying[]上使用contains运算符

[Not sure, if it is any kind of bug] [不确定,如果它是任何一种错误]

Modifying the Character varying[] array to text[] in postgres fixed the issue.在 postgres 中将Character varying[]数组修改为text[]解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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