我总是用来返回行数,使用foreach循环。 在项目的一部分中,我需要获得一个包含动态数据的过程。 程序代码:
ALTER procedure [dbo].[abcde]
@fldStatusIdString nvarchar(max)
as
begin
declare @Query nvarchar(max)
set @Query = '
SELECT a.fldEstateId, a.fldEstateTitle, a.fldThumbnail,a.fldPrice,fldAcceptDate
FROM (
SELECT dbo.tblImages.fldEstateId, dbo.tblImages.fldThumbnail, dbo.tblEstates.fldEstateTitle,
dbo.tblEstates.fldPrice, dbo.tblEstates.fldAcceptDate, ROW_NUMBER() OVER (PARTITION BY dbo.tblImages.fldEstateId
ORDER BY dbo.tblEstates.fldAcceptDate) AS RowNumber
FROM dbo.tblImages INNER JOIN dbo.tblEstates ON dbo.tblEstates.fldEstateId = dbo.tblImages.fldEstateId
WHERE (dbo.tblEstates.fldStatusOfAccept = 1)'
set @Query +='and (fldStatusId in('+@fldStatusIdString+'))
) AS a
WHERE a.RowNumber = 1
order by fldEstateId
'
execute sp_executesql @Query
end
当我使用上面的程序时,不能在foreach循环中使用ToList。 如果正常的程序不是问题,但如果我使用上面的方法(输入数据与变量的数量),我得到一个错误。 C#代码:
List<Join_Estates_Images_Model> ListAll = new List<Join_Estates_Images_Model>();
foreach (var item in db.abcde(fldStatusIdString).ToList())
{
Join_Estates_Images_Model _entity = new Join_Estates_Images_Model();
_entity.fldEstateId = item.fldEstateId;
_entity.fldEstateTitle = item.fldEstateTitle;
_entity.fldThumbnail = item.fldThumbnail;
_entity.fldPrice = Convert.ToDecimal(item.fldPrice);
_entity.fldAcceptDate = item.fldAcceptDate;
ListAll.Add(_entity);
}
return ListAll;
错误讯息:
Error 14 'int' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)
我使用'使用System.Linq'
答案 0 :(得分:0)
我在实体框架面临着同样的问题。实体框架始终使用动态查询提供整数结果集。请按照以下步骤
更新您的模型
答案 1 :(得分:0)
另一种方法是将逗号分隔列表传递给变量并在过程中处理它......就像......
此外你不需要动态sql .....
ALTER procedure [dbo].[abcde]
@fldStatusIdString nvarchar(max)
as
begin
DECLARE @xml XML;
SET @xml = N'<t>' + REPLACE(@fldStatusIdString,',','</t><t>') + '</t>';
SELECT a.fldEstateId, a.fldEstateTitle, a.fldThumbnail,a.fldPrice,fldAcceptDate
FROM (
SELECT dbo.tblImages.fldEstateId
, dbo.tblImages.fldThumbnail
, dbo.tblEstates.fldEstateTitle
, dbo.tblEstates.fldPrice
, dbo.tblEstates.fldAcceptDate
, ROW_NUMBER() OVER (PARTITION BY dbo.tblImages.fldEstateId
ORDER BY dbo.tblEstates.fldAcceptDate) AS RowNumber
FROM dbo.tblImages
INNER JOIN dbo.tblEstates
ON dbo.tblEstates.fldEstateId = dbo.tblImages.fldEstateId
WHERE (dbo.tblEstates.fldStatusOfAccept = 1)
and (fldStatusId in(
SELECT r.value('.','varchar(MAX)') as item
FROM @xml.nodes('/t') as records(r)
)
)
) AS a
WHERE a.RowNumber = 1
order by fldEstateId
end