查询返回 SQL 表达式而不是数据库中的值?

发布于 2024-12-04 10:38:00 字数 596 浏览 0 评论 0原文

我的数据库表中有一个整数列,我想选择它,但它给出了无法从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命比纸薄 2024-12-11 10:38:00

try

int Category_ID = (from p in StaticVa.new_data_context.Categories
                              where p.CategoryName == Category_Name
                              select p.CategoryID).FirstOrDefault();

LINQ 尝试返回与 where 子句匹配的每个 id 的 IQueryable,如果您只想要一个,则需要将其指定为 single 或 first

try

int Category_ID = (from p in StaticVa.new_data_context.Categories
                              where p.CategoryName == Category_Name
                              select p.CategoryID).FirstOrDefault();

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

渔村楼浪 2024-12-11 10:38:00
(from p in StaticVa.new_data_context.Categories
                              where p.CategoryName == Category_Name
                              select p.CategoryID)

表示一个表达式,执行时将返回一组项目。

所以你说:

var catIds = (from p in StaticVa.new_data_context.Categories
                              where p.CategoryName == Category_Name
                              select p.CategoryID);

然后你得到第一个(即强制执行上述表达式并开始返回值):

int? Category_ID = catIds.FirstOrDefault();

你需要 int? 以防万一没有返回第一行,其中case FirstOrDefault() 将返回 null。

但是,如果您确定它返回至少一行,那么您可以说

int Category_ID = catIds.First();

如果您确定它返回仅一行,那么您可以说

int Category_ID = catIds.Single();
(from p in StaticVa.new_data_context.Categories
                              where p.CategoryName == Category_Name
                              select p.CategoryID)

represents an expression, that when executed, will return a set of items.

so you say:

var catIds = (from p in StaticVa.new_data_context.Categories
                              where p.CategoryName == Category_Name
                              select p.CategoryID);

then you get the for first one (ie. force the above expression to be execute and start returning values) by:

int? Category_ID = catIds.FirstOrDefault();

you need the int? in case there is no first row returned in which case FirstOrDefault() will return a null.

However if you're sure it return atleast one row then you can say

int Category_ID = catIds.First();

And if you're sure it return only one row then you can say

int Category_ID = catIds.Single();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文