无法将类型为“ System.Single”的对象转换为类型为“ System.Double”的对象。

时间:2020-07-23 13:03:48

标签: c# sql database entity-framework

我目前正在编写一个跟踪某些股票的小应用程序,我有一个包含股票数据的数据库,想将其放入WPF数据网格,但是出现以下错误:

无法将类型为“ System.Single”的对象转换为类型为“ System.Double”

原谅我不好的编码技巧,因为几年来我还没有这样做,所以我看了一下内部异常,但它只是说空。

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            using (AppDbContext db = new AppDbContext())
            {
                StockGrid.ItemsSource = db.LowFloatStocks.ToList();
            }
        }
    }
}

Stock.cs

#region Day 1 Open Variabes

private int stockId;
private string stockTicker;
private double openingPrice;
private double spike;
private double high;
private double low;
private double closingPrice;

private double gainPercent;
private double gapPercent;
private double highPercent;
private double lowPercent;
private double closePercent;

private string greenToRed;
private string redToGreen;
private string closeLessEqualToOpen;
private string closeRed;
private double closeVHigh;
private double closeVOpen;

private string catalyst;
private double stockFloat;
private string dilution;
#endregion


#region Day 2 Open Variables
private double day2Open;
//private decimal day2Spike;
private double day2High;
private double day2Low;
private double day2Close;

private double day2gapPercent;
private double day2highPercent;
private double day2lowPercent;
private double day2ClosePercent;

private string day2GapUp;
private string day2GapDown;
private string day2GreenRed;
private string day2RedGreen;

private string day2CloseLessEqualOpen;
private string day2CloseRed;
private double day2CloseVHigh;
private double day2ClosevOpen;

SQL

CREATE TABLE [dbo].[LowFloatStocks] (
    [Id]                       INT           NOT NULL,
    [Date]                     DATE          NOT NULL,
    [Ticker]                   NCHAR (10)    NOT NULL,
    [ClosingPrice]             REAL          NOT NULL,
    [OpeningPrice]             REAL          NOT NULL,
    [GainPercent]              AS            (round(([High]-[OpeningPrice])/[OpeningPrice],(4))*(100.0)) PERSISTED NOT NULL,
    [GapPercent]               AS            (round(([OpeningPrice]-[ClosingPrice])/[ClosingPrice],(4))*(100.0)) PERSISTED NOT NULL,
    [Spike]                    REAL          NOT NULL,
    [1stSpike%]                AS            (round(([Spike]-[OpeningPrice])/[OpeningPrice],(4))*(100.0)) PERSISTED NOT NULL,
    [High]                     REAL          NOT NULL,
    [HighPercent]              AS            (round(([High]-[ClosingPrice])/[ClosingPrice],(4))*(100.0)) PERSISTED NOT NULL,
    [Low]                      REAL          NOT NULL,
    [LowPercent]               AS            (round(([Low]-[ClosingPrice])/[ClosingPrice],(4))*(100.0)) PERSISTED NOT NULL,
    [1stClose]                 REAL          NOT NULL,
    [ClosePercent]             AS            (round(([1stClose]-[ClosingPrice])/[ClosingPrice],(4))*(100.0)) PERSISTED NOT NULL,
    [greenToRed]               NCHAR (3)     NOT NULL,
    [redToGreen]               NCHAR (3)     NOT NULL,
    [CloseLessEqualToOpen]     NCHAR (3)     NOT NULL,
    [CloseRed]                 NCHAR (3)     NOT NULL,
    [ClosevHigh]               AS            (round(([High]-[1stClose])/[1stClose],(4))*(100)) PERSISTED NOT NULL,
    [ClosevOpen]               AS            (round(([OpeningPrice]-[1stClose])/[OpeningPrice],(4))*(100.0)) PERSISTED NOT NULL,
    [Catalyst]                 NVARCHAR (50) NOT NULL,
    [Float]                    DECIMAL (18)  NOT NULL,
    [Dilution]                 NCHAR (3)     NOT NULL,
    [Day2Open]                 REAL          NOT NULL,
    [day2gapPercent]           AS            (round(([Day2Open]-[1stClose])/[1stClose],(4))*(100.0)) PERSISTED NOT NULL,
    [Day2High]                 REAL          NOT NULL,
    [day2HighPercent]          AS            (round(([Day2High]-[1stClose])/[1stClose],(4))*(100)) PERSISTED NOT NULL,
    [Day2Low]                  REAL          NOT NULL,
    [Day2LowPercent]           AS            (round(([Day2Low]-[1stClose])/[1stClose],(4))*(100.0)) PERSISTED NOT NULL,
    [Day2Close]                REAL          NOT NULL,
    [Day2ClosePercent]         AS            (round(([Day2Close]-[1stClose])/[1stClose],(4))*(100.0)) PERSISTED NOT NULL,
    [Day2GapUp]                NCHAR (3)     NOT NULL,
    [Day2GapDown]              NCHAR (3)     NOT NULL,
    [Day2GreenToRed]           NCHAR (3)     NOT NULL,
    [Day2RedToGreen]           NCHAR (3)     NOT NULL,
    [Day2CloseLessEqualToOpen] NCHAR (3)     NOT NULL,
    [Day2CloseRed]             NCHAR (3)     NOT NULL,
    [Day2ClosevHigh]           AS            (round(([Day2Open]-[Day2High])/[Day2Open],(4))*(100.0)) PERSISTED NOT NULL,
    [Day2ClosevOpen]           AS            (round(([Day2Open]-[Day2Close])/[Day2Open],(4))*(100.0)) PERSISTED NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

1 个答案:

答案 0 :(得分:1)

您可以将所有DBType从REAL更改为FLOAT,也可以将C#类型从double更改为float

DBType REAL is float in C#
DBType FLOAT is double in C#