我有以下Quote和Share类:

public class Quote
{
    public Quote(string ticker, string name, decimal lastPrice)
    {
        this.Ticker = ticker;
        this.Name = name;
        this.LastPrice = lastPrice;
    }

    public string Ticker { get; private set; }

    public string Name { get; private set; }

    public decimal LastPrice { get; private set; }
}

public class Share
{
    public Share(Quote quote, int quantity)
    {
        this.Quote = quote;
        this.Quantity = quantity;
    }

    public Quote Quote { get; private set; }
    public int Quantity { get; private set; }
}

这些表:

CREATE TABLE [dbo].[quotes](
[ticker] [char](5) NOT NULL,
[name] [varchar](60) NOT NULL,
[last_price] [money] NOT NULL,
[bid_price] [money] NULL,
[bid_quantity] [int] NULL,
[ask_price] [money] NULL,
[ask_quantity] [int] NULL,
[high] [money] NULL,
[low] [money] NULL,
[previous_close] [money] NULL,
[created_by] [varchar](12) NULL,
[created_date] [datetime] NULL,
[modified_by] [varchar](12) NULL,
[modified_date] [datetime] NULL,
CONSTRAINT [PK_quotes] PRIMARY KEY CLUSTERED 
(
[ticker] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  =     ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[shares](
[ticker] [char](5) NOT NULL,
[broker_id] [char](12) NOT NULL,
[quantity] [int] NOT NULL,
[created_by] [varchar](12) NOT NULL,
[created_date] [datetime] NOT NULL,
[modified_by] [varchar](12) NULL,
[modified_date] [datetime] NULL,
CONSTRAINT [PK_shares] PRIMARY KEY CLUSTERED 
(
[ticker] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,            IGNORE_DUP_KEY        =     OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)     ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[shares]  WITH CHECK ADD  CONSTRAINT [FK_shares_quotes]     FOREIGN KEY([ticker])
REFERENCES [dbo].[quotes] ([ticker])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[shares] CHECK CONSTRAINT [FK_shares_quotes]
GO

最后是这段代码:

        string sql = "SELECT quantity, s.ticker, name, last_price FROM shares s " +
                     "INNER JOIN quotes q ON q.ticker = s.ticker " +
                     "WHERE s.ticker=@ticker AND broker_id = @broker_Id";

        IEnumerable<Share> shares = connection.Query<Share, Quote, Share>(sql,
                                        (s, q) =>
                                        {
                                            Share share = new Share(q, s.Quantity);
                                            return share;
                                        },
                                        new { ticker = ticker, broker_Id = brokerId }
                                        , splitOn: "ticker");

        return shares.FirstOrDefault();

执行该查询时,出现以下错误:

“不支持指定的方法”(???)

如果我通过以下方式修改sql语句(“ last_price作为lastPrice”):

string sql = "SELECT quantity, s.ticker, name, last_price as lastPrice FROM shares s " +
                     "INNER JOIN quotes q ON q.ticker = s.ticker " +
                     "WHERE s.ticker=@ticker AND broker_id = @broker_Id";

我收到以下错误:

“ Footstock.Domain.Model.Quote实现需要一个无参数的默认构造函数或一个匹配的签名(System.String代码,System.String名称,System.Decimal lastPrice)”

我究竟做错了什么?

===============>>#1 票数:0 已采纳

对于Share类,您需要添加无参数构造函数或仅接受quantity参数的构造函数。

在Dapper调用组成返回结果的方法之前,它需要构建传递给(s, q) => ...在您的情况下,您需要一个ShareQuote类,这两个类都必须具有可以使用Dapper拥有的数据调用的构造函数。 对于Quote ,构造函数匹配查询中的属性。 对于Share ,Dapper仅具有quantity并且还没有Quote对象。

  ask by Kevin translate from so

未解决问题?本站智能推荐: