显式列表转换类型错误?
我正在尝试从存储过程的结果转换为列表。我已经为 timerangeResult -> 的单个对象创建了显式(工作)转换。预订,但我缺少清单..
这是代码:
public static explicit operator List<Booking>(timerangeResult t)
{
List<Booking> bL = new List<Booking>();
IEnumerable<timerangeResult> tx = (IEnumerable<timerangeResult>) t;
foreach (timerangeResult tt in tx)
{
Booking b = (Booking)tt;
bL.Add(b);
}
//return bL;
//return new List<Booking>(bL);
//return new List<Booking>(IEnumerable < Booking > bL);
return bL;
// [NONE OF THESE WORK]
// ERROR:
// User-defined conversion must convert to or from the enclosing type (UNDERLINED: "explicit operator List<Booking>" line 1)
}
提前致谢!
I'm trying to cast to a List from a result from a stored procedure .. I have already created the explicit (working) cast for a single object of timerangeResult -> Booking, but I am missing a list ..
Here's the code:
public static explicit operator List<Booking>(timerangeResult t)
{
List<Booking> bL = new List<Booking>();
IEnumerable<timerangeResult> tx = (IEnumerable<timerangeResult>) t;
foreach (timerangeResult tt in tx)
{
Booking b = (Booking)tt;
bL.Add(b);
}
//return bL;
//return new List<Booking>(bL);
//return new List<Booking>(IEnumerable < Booking > bL);
return bL;
// [NONE OF THESE WORK]
// ERROR:
// User-defined conversion must convert to or from the enclosing type (UNDERLINED: "explicit operator List<Booking>" line 1)
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
需要在所涉及的两种类型之一中定义强制转换运算符。要么是源操作数类型,要么是目标操作数类型。
换句话说,您需要在以下位置定义运算符:
List
(这是不可能的)timerangeResult
我的猜测是您已经定义了运算符在其他地方,尝试将其移至
timerangeResult
类型。另请注意,显式运算符很难发现,您确实需要知道它们的存在。通常最好添加一个执行相同操作的实例方法,即:
A cast operator needs to be defined in one of the two types involved. Either in the source operand type, or in the destination operand type.
In other words, you need to define the operator either in:
List<Booking>
(which is impossible)timerangeResult
My guess is that you've defined the operator somewhere else, try moving it to the
timerangeResult
type.Also, note that explicit operators are hard to discover, you really need to know they're there. It's usually much better to add an instance method that does the same thing, ie.:
运算符定义在什么类型中?该运算符只能在
timerangeResult
类中定义(因为另一个选项List
不在您的控制范围内)What type is the operator defined in? That operator can only be defined in the
timerangeResult
class (sinceList<Booking>
, the other option, is outside your control)这就是我现在得到的(db.designer.cs)
和.. Booking.cs
现在我得到:“查询结果不能被枚举多次”
This is what I got now (db.designer.cs)
and .. Booking.cs
Now I get: "The query results cannot be enumerated more than once."