将Generic.List <int?>转换为Generic.List <int>

Jon*_*ing 4 c# linq stored-procedures

我从存储过程返回结果集.它是一个发送回整数列表的临时表.

当我尝试返回结果时,我收到一个错误 Generic.List<int?> to Generic.List<int>

这就是我正在尝试的:

using (SecurityEntities ctx = new SecurityEntities())
{
    List<int> newList = ctx.spStoreSearch(storeNumber).Where(x => x != null).Select(x => x).ToList();
   return test;
}
Run Code Online (Sandbox Code Playgroud)

根据ctx.spStoreSearch(storeNumber).Where它说的部分Method, Delegate or event is expected

我基于我目前在这个答案上所做的工作

我的错误可能在存储过程本身吗?这就是我从storedProc返回的内容select * from @TempTable

Hab*_*bib 15

选择Nullable int的值如:

.Select(x => x.Value)
Run Code Online (Sandbox Code Playgroud)

你也可以做像:

.Select(x => (int) x)
Run Code Online (Sandbox Code Playgroud)

您的查询可能是:

List<int> newList = ctx.spStoreSearch(storeNumber)
                        .Where(x => x.HasValue)
                        .Select(x => x.Value).ToList();
Run Code Online (Sandbox Code Playgroud)

你得到的异常,因为你在元素List的类型是int?Nullable<int>,所以当你做的Select(x=> x)是选择类型的项目int?,你不能分配给List<int>.

  • 如果是这种情况,错误信息不应该不同吗? (2认同)