gpt4 book ai didi

c# - 将 Generic.List 转换为 Generic.List

转载 作者:太空狗 更新时间:2023-10-30 00:04:51 25 4
gpt4 key购买 nike

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

当我尝试返回结果时出现错误 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;
}

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

我基于我目前所做的 this answer

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

最佳答案

选择 Nullable int 的值,例如:

.Select(x => x.Value)

您也可以像这样进行转换:

.Select(x => (int) x)

您的查询可以是:

List<int> newList = ctx.spStoreSearch(storeNumber)
.Where(x => x.HasValue)
.Select(x => x.Value).ToList();

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

关于c# - 将 Generic.List<int?> 转换为 Generic.List<int>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26001508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com