结果中不存在必需的列“int_CommodityPriceId”。 linq to sql

时间:2011-09-16 11:08:13

标签: sql linq linq-to-sql

public IEnumerable<CommodityPrice> GetAllTimeFrameDaily()
    {
        return _dbprice.ExecuteQuery<CommodityPrice>("SELECT MIN(CommodityPrice.dtm_Date),MAX(CommodityPrice.dtm_Date) FROM CommodityPrice WHERE (CommodityPrice.int_FrequencyId = 1) and int_MarketLocationId in (SELECT [int_LocationId] FROM [Location] where int_LocationTypeId=4  and int_ParentLocationId in (SELECT [int_LocationId] FROM [Location] where int_LocationTypeId=2 and int_ParentLocationId in (SELECT int_LocationId FROM [Location] where int_LocationTypeId = 1 and vcr_LocationEn='Pakistan')))");
    }

我收到此错误The required column 'int_CommodityPriceId' does not exist in the results.知道为什么会这样做。 (我的dbml文件确实有CommodityPriceId列)。我正在使用linq to sql和vs 2010.running它在sql server中运行文件

commodityprice

int_CommodityPriceId bigint Unchecked
int_CommodityId bigint  Checked
int_SourcesId   int Checked
int_MarketLocationId    int Checked
int_FrequencyId int Checked
flt_Price   float   Checked
flt_PriceUSD    float   Checked

1 个答案:

答案 0 :(得分:2)

此代码尝试将结果分配给CommodityPrice实体。 为此,它需要返回CommodityPrice实体列属性中定义的所有列。您的查询不会返回任何列。

如果您只想返回这两个字段,则需要一个仅包含2个日期字段的新实体。

TimeFrame
dte_CommodityPriceMinDate  datetime  Unchecked
dte_CommodityPriceMaxDate  datetime  Checked

并且查询应填充此内容:

public IEnumerable<TimeFrame> GetAllTimeFrameDaily()
    {
        return _dbprice.ExecuteQuery<TimeFrame>("SELECT MIN(CommodityPrice.dtm_Date) AS dte_CommodityPriceMinDate ,MAX(CommodityPrice.dtm_Date) AS dte_CommodityPriceMaxDate FROM CommodityPrice WHERE (CommodityPrice.int_FrequencyId = 1) and int_MarketLocationId in (SELECT [int_LocationId] FROM [Location] where int_LocationTypeId=4  and int_ParentLocationId in (SELECT [int_LocationId] FROM [Location] where int_LocationTypeId=2 and int_ParentLocationId in (SELECT int_LocationId FROM [Location] where int_LocationTypeId = 1 and vcr_LocationEn='Pakistan')))");
    }