[英]Converting string to int in linq
I have a table tblcatalogue and there is a column classid with datatype varchar.I want a result set containing orderby classid ,but the problem is using order by classid is not giving the desired result.so i tried to convert classid to integer like 我有一个表tblcatalogue,并且有一个数据类型为varchar的列classid。我想要一个包含orderby classid的结果集,但是问题是使用order by classid并没有给出期望的结果。所以我试图将classid转换为整数
var query= itemRepository.GetAll();
results = query.ToList().OrderBy(x=> Convert.ToInt32(x.ClassId));
but I am getting sql query like 但是我正在像这样的SQL查询
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[SubjectId] AS [SubjectId],
[Extent1].[SeriesId] AS [SeriesId],
[Extent1].[Title] AS [Title],
[Extent1].[Author] AS [Author],
[Extent1].[ISBN] AS [ISBN],
[Extent1].[Edition] AS [Edition],
[Extent1].[Price] AS [Price],
[Extent1].[Discount] AS [Discount],
[Extent1].[BoardId] AS [BoardId],
[Extent1].[ClassId] AS [ClassId]
FROM [dbo].[tblCataLogue] AS [Extent1]
ORDER BY [Extent1].[ClassId] ASC
I wanted converted sql query like 我想要像这样的转换SQL查询
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[SubjectId] AS [SubjectId],
[Extent1].[SeriesId] AS [SeriesId],
[Extent1].[Title] AS [Title],
[Extent1].[Author] AS [Author],
[Extent1].[ISBN] AS [ISBN],
[Extent1].[Edition] AS [Edition],
[Extent1].[Price] AS [Price],
[Extent1].[Discount] AS [Discount],
[Extent1].[BoardId] AS [BoardId],
[Extent1].[ClassId] AS [ClassId]
FROM [dbo].[tblCataLogue] AS [Extent1]
ORDER BY covnert(int,[Extent1].[ClassId]) ASC
Because it is giving the right result set 因为它给出了正确的结果集
When using ToList()
you are executing the query to the database and therefore the ordering you want does not take place. 使用
ToList()
您正在执行对数据库的查询,因此不会发生所需的排序。 Remove ToList()
and the order by clause will also be translated to sql: 删除
ToList()
,order by子句也将转换为sql:
results = query.OrderBy(x=> Convert.ToInt32(x.ClassId)).ToList();
As for the proceeding error, the Convert.ToInt32
is not supported by the linq provider and cannot be translated to sql. 至于继续发生的错误,linq提供程序不支持
Convert.ToInt32
,并且不能将其转换为sql。 Therefore you should do one of two things: 因此,您应该执行以下两项操作之一:
ToList()
/ AsEnumerable()
) and then convert ToList()
/ AsEnumerable()
),然后进行转换
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.