Linq:用2个不同的语句映射pro到函数

时间:2012-08-23 06:36:14

标签: sql linq

我制作存储过程,选择条件数据,但问题是: -

当RequiredIDID = 2时(选择来自连接表的一些数据,如果不是我不在else语句中选择它),并且在映射到linq函数后出现此问题

如果我将requirdID = 2传递给函数没有问题,则执行proc中的第一个选择,但如果我传递其他requirdID(例如= 3),则会出现此消息

  

数据阅读器与指定的内容不兼容   'RealEstateCenterModel.SearchForRealState_Reader'。一个成员   类型'FloorTypeCategory',中没有相应的列   具有相同名称的数据阅读器。

USE [RealEstateCenter]
GO
/****** Object:  StoredProcedure [dbo].[TestOptionalParameter]    Script Date: 08/23/2012 07:29:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[TestOptionalParameter]
@RequiredIDID int =NULL 
as

if @RequiredIDID=2
begin
SELECT     FloorSpace.SpaceFrom, RealEstateCategoryDetails.RequiredCategoryTypeName, FloorType.FloorTypeCategory, FloorType.FloorNotes, 
                      RealEstateTypes.RequiedType
FROM         PersonalRealStateDetails INNER JOIN
                      FloorSpace ON PersonalRealStateDetails.FloorSpaceID = FloorSpace.FloorSpaceID INNER JOIN
                      RealEstateCategoryDetails ON PersonalRealStateDetails.RequiredCategoryTypeID = RealEstateCategoryDetails.RequiredCategoryTypeID INNER JOIN
                      FloorType ON PersonalRealStateDetails.FloorTypeID = FloorType.FloorTypeID INNER JOIN
                      RealEstateTypes ON RealEstateCategoryDetails.RequiredID = RealEstateTypes.RequiredID

end

else
begin
SELECT     FloorSpace.SpaceFrom, RealEstateCategoryDetails.RequiredCategoryTypeName, RealEstateTypes.RequiedType
FROM         PersonalRealStateDetails INNER JOIN
                      FloorSpace ON PersonalRealStateDetails.FloorSpaceID = FloorSpace.FloorSpaceID INNER JOIN
                      RealEstateCategoryDetails ON PersonalRealStateDetails.RequiredCategoryTypeID = RealEstateCategoryDetails.RequiredCategoryTypeID INNER JOIN
                      RealEstateTypes ON RealEstateCategoryDetails.RequiredID = RealEstateTypes.RequiredID
end

我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以将缺少的列(包含一些默认值)添加到第二个选择。

SELECT FloorSpace.SpaceFrom, RealEstateCategoryDetails.RequiredCategoryTypeName, RealEstateTypes.RequiedType, 0 as [FloorType.FloorTypeCategory], NULL as [FloorType.FloorNotes]

SELECT FloorSpace.SpaceFrom, RealEstateCategoryDetails.RequiredCategoryTypeName, RealEstateTypes.RequiedType, 0 as FloorTypeCategory, NULL as FloorNotes