Linq:使用2个不同的语句将proc映射到函数 [英] Linq : mapping proc to fuction with 2 different statement
问题描述
我使存储过程有条件地选择数据,但问题是:-
当RequiredIDID = 2(从联接表中选择一些数据,如果不是,我不在else语句中选择它)并且映射到linq函数后,出现此问题
如果我将requirdID = 2传递给函数,则不会发生任何问题,因为在执行过程中执行了第一个选择,但是如果我传递了其他requirdID(例如= 3),则出现此消息
数据读取器与指定的"RealEstateCenterModel.SearchForRealState_Reader"不兼容.类型成员"FloorTypeCategory"在数据读取器中没有具有相同名称的对应列.
I make stored procedure that select data with condition but the problem is :-
when RequiredIDID =2 ( Select come with some data from join table and if not I am not select it in else statement ) and after mapping to linq function this problem appears
if I''m pass requirdID = 2 to function no problem occurs coz the first select in proc executed but if i pass other requirdID ( ex. = 3 ) this message appears
The data reader is incompatible with the specified ''RealEstateCenterModel.SearchForRealState_Reader''. A member of the type, ''FloorTypeCategory'', does not have a corresponding column in the data reader with the same name.
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
推荐答案
我会说引发了错误,因为当@RequiredIDID!= 2时,您生成的查询仅提供3列.C#分析了存储的程序,并且总是期望有5列.
这可以通过在@RequiredIDID不等于2时提供2个额外的列来解决.例如,将代码的ELSE部分更改为:
I would say that the error is raised, because your resulting query supplies only 3 columns when the @RequiredIDID != 2. C# has analyzed the stored procedure and always expects 5 columns.
This could be solved by supplying 2 extra columns when the @RequiredIDID is not equal to 2. For instance change the ELSE part of your code to:
else
begin
SELECT FloorSpace.SpaceFrom, RealEstateCategoryDetails.RequiredCategoryTypeName,
NULL as FloorTypeCategory, NULL as FloorNotes,
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
这应该使代码正常工作.我希望它能提供理想的结果.
This should make the code work. I hope it give the desired result.
这是两个不同的查询,基于您要传入的参数.您应该执行两个不同的查询,并在其中进行切换基于参数的C#,例如
It''s two different queries, based on a parameter you''re passing in. You should just do two different queries and switch between them in the C# based on the parameter, e.g.
if(RequiredID == 2){
var answer = select from ... (first query)
} else {
var answer = select from ... (other query)
}
我猜一个人将不得不返回一个类型列表,该类型是另一个的子类,因为有额外的属性.然后,您必须考虑如何在以后的应用程序中访问这些属性.
I guess one will have to return a list of a type that is a subclass of the other, as there are extra properties. Then you have to think about how you can access those properties later in your application.
这篇关于Linq:使用2个不同的语句将proc映射到函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!