显式列表转换类型错误?

发布于 2024-10-06 02:57:24 字数 766 浏览 5 评论 0原文

我正在尝试从存储过程的结果转换为列表。我已经为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放飞的风筝 2024-10-13 02:57:24

需要在所涉及的两种类型之一中定义强制转换运算符。要么是源操作数类型,要么是目标操作数类型。

换句话说,您需要在以下位置定义运算符:

  • List (这是不可能的)
  • ...或 timerangeResult

我的猜测是您已经定义了运算符在其他地方,尝试将其移至 timerangeResult 类型。

另请注意,显式运算符很难发现,您确实需要知道它们的存在。通常最好添加一个执行相同操作的实例方法,即:

public class timerangeResult
{
    ...

    public List<Booking> ToBookingList()
    {
        ...
    }
}

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)
  • ... or 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.:

public class timerangeResult
{
    ...

    public List<Booking> ToBookingList()
    {
        ...
    }
}
违心° 2024-10-13 02:57:24

运算符定义在什么类型中?该运算符只能在 timerangeResult 类中定义(因为另一个选项 List 不在您的控制范围内)

What type is the operator defined in? That operator can only be defined in the timerangeResult class (since List<Booking>, the other option, is outside your control)

木槿暧夏七纪年 2024-10-13 02:57:24

这就是我现在得到的(db.designer.cs)

public partial class timerangeResult
{

    private int _ID;

    private string _Login;

    private System.DateTime _Starts;

    private System.DateTime _Ends;

    private string _Delete;

    private byte _Notify;

// CUSTOM
//public static explicit operator List<Booking>(timerangeResult t)
public static List<Booking> ToBookingList(IEnumerable<timerangeResult> t)
{
    List<Booking> bL = new List<Booking>();

    foreach (timerangeResult tt in t)
    {
        Booking b = (Booking)tt;
        bL.Add(b);
    }
    return bL;
}
// CUSTOM END^

和.. Booking.cs

public static List<Booking> Today(DateTime begin, DateTime end)
{
    try
    {
        dbDataContext db = new dbDataContext();

        IEnumerable<timerangeResult> bQ = from b in db.timerange(begin, end)
                             select b;
        List<Booking> bL = timerangeResult.ToBookingList(bQ);

        return bL;
    }
    catch (Exception)
    {
        throw;
    }
}

现在我得到:“查询结果不能被枚举多次”

This is what I got now (db.designer.cs)

public partial class timerangeResult
{

    private int _ID;

    private string _Login;

    private System.DateTime _Starts;

    private System.DateTime _Ends;

    private string _Delete;

    private byte _Notify;

// CUSTOM
//public static explicit operator List<Booking>(timerangeResult t)
public static List<Booking> ToBookingList(IEnumerable<timerangeResult> t)
{
    List<Booking> bL = new List<Booking>();

    foreach (timerangeResult tt in t)
    {
        Booking b = (Booking)tt;
        bL.Add(b);
    }
    return bL;
}
// CUSTOM END^

and .. Booking.cs

public static List<Booking> Today(DateTime begin, DateTime end)
{
    try
    {
        dbDataContext db = new dbDataContext();

        IEnumerable<timerangeResult> bQ = from b in db.timerange(begin, end)
                             select b;
        List<Booking> bL = timerangeResult.ToBookingList(bQ);

        return bL;
    }
    catch (Exception)
    {
        throw;
    }
}

Now I get: "The query results cannot be enumerated more than once."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文