查询返回 SQL 表达式而不是数据库中的值?
我的数据库表中有一个整数列,我想选择它,但它给出了无法从 int 转换为字符串的异常
示例我想从 Category 中选择
表,其中 CategoryID
CategoryName="Cat"
categoryID
为 int & categoryName
是
我尝试过的 linq 查询的
string Category_ID = (from p in StaticVa.new_data_context.Categories
where p.CategoryName == Category_Name
select p.CategoryID).ToString();
字符串,它返回 "SELECT [t0].[CategoryID]\r\nFROM [dbo].[Category] AS [t0]\r\nWHERE [ t0].[类别名称] = @p0\r\n"
I have an integer column in my table in the database, and I want to select it but it give me exception that can't convert from int to string
Example I want to select CategoryID
from Category
table where CategoryName="Cat"
categoryID
is int & categoryName
is string
my linq query which I tried
string Category_ID = (from p in StaticVa.new_data_context.Categories
where p.CategoryName == Category_Name
select p.CategoryID).ToString();
It returned "SELECT [t0].[CategoryID]\r\nFROM [dbo].[Category] AS [t0]\r\nWHERE [t0].[CategoryName] = @p0\r\n"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
try
LINQ 尝试返回与 where 子句匹配的每个 id 的 IQueryable,如果您只想要一个,则需要将其指定为 single 或 first
try
LINQ is trying to return a IQueryable of every id which matches the where clause, if you only want one you need to specify it as single or first
表示一个表达式,执行时将返回一组项目。
所以你说:
然后你得到第一个(即强制执行上述表达式并开始返回值):
你需要
int?
以防万一没有返回第一行,其中caseFirstOrDefault()
将返回 null。但是,如果您确定它返回至少一行,那么您可以说
如果您确定它返回仅一行,那么您可以说
represents an expression, that when executed, will return a set of items.
so you say:
then you get the for first one (ie. force the above expression to be execute and start returning values) by:
you need the
int?
in case there is no first row returned in which caseFirstOrDefault()
will return a null.However if you're sure it return atleast one row then you can say
And if you're sure it return only one row then you can say