【发布时间】:2013-09-20 19:06:36
【问题描述】:
我有一个包含这些列(类型)的表格,如下所述。
表格
------------------------------------------------------------------
Dir(str) | Twnshp(int) | Rng(int) | Section(int) | Xcell(int) | Ycell(int)
------------------------------------------------------------------
我正在尝试使用 EF 进行此查询。
SELECT Xcell,Ycell FROM [CIR].[dbo].[TRS2Cell] where Twnshp = 1 and Rng = 4 and Section =31
经过一番研究,我创建了一个 DAL 上下文和类,如下所示。
PlotXYContext.cs
public class PlotXYContext :DbContext
{
public DbSet<PlotXY> XYCells { get; set; }
}
PlotXY.cs
[Table("TRS2Cell")]
public class PlotXY
{
public string Dir { get; set; }
[Key]
public int Twnshp { get; set; }
public int Rng { get; set; }
public int Section { get; set; }
public int Xcell { get; set; }
public int Ycell { get; set; }
}
这是我传递三个参数的控制器中的代码。
PlotXYContext plotXYContext = new PlotXYContext();
var query = from TRS2Cell in plotXYContext.XYCells
where TRS2Cell.Twnshp == 1
&& TRS2Cell.Rng == 4
&& TRS2Cell.Section == 31
select TRS2Cell.Xcell;
我需要 EF 方面的帮助,因为我是新手,这也是正确的查询吗? 如果是这样,我如何从查询中检索 Xcell 和 Ycell 值。 此外,该表没有唯一列,没有空值,这里不需要更新任何内容。我只想做一个选择。
【问题讨论】:
-
我不知道我是否理解正确。在我看来,正确的工作查询返回您的 ORM 类的实例,其中 Twnshp 为 1,Rng 为 4,Section 为 31。
-
当我调试并查看“查询”时,它会抛出这个错误“从物化 'System.String' 类型到 'System.Int32' 类型的指定转换无效。'
-
哪家生产线?
-
没有编译错误
-
让我重新表述一下,我尝试调试并检查查询是否返回任何内容,结果在“var query”上发现了此异常。
标签: c# sql-server asp.net-mvc linq entity-framework