【问题标题】:Linq Stored Procedure Issue- Returning an intLinq 存储过程问题 - 返回一个 int
【发布时间】:2012-12-03 12:00:13
【问题描述】:

我正在尝试使用 Linq 调用存储过程,该存储过程在 SQL 中返回的值很好,但是当我将它拖到我的 DBML 文件并尝试从我的代码中调用它时,它返回了

找不到源类型“int”的查询模式的实现。未找到“选择”。

我查看了我拥有的其他线程和其他存储过程,但由于某种原因而不是使用ISingleResult,这是不同的,我似乎也无法更改返回类型。

这是后面的 DBML 代码

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.displayDetails")]
public int displayDetails([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="VarChar(1)")] string sex, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> day, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> month, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> year, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="PostCode", DbType="VarChar(10)")] string postCode, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="AppTime", DbType="DateTime")] System.Nullable<System.DateTime> appTime, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> filter)
{
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), sex, day, month, year, postCode, appTime, filter);
        return ((int)(result.ReturnValue));
}

我的页面代码出现错误

    var person = from p in db.displayDetails(sex.ToString(),
                  Convert.ToInt32(dayOfBirth),
                  Convert.ToInt32(monthOfBirth),
                  Convert.ToInt32(yearOfBirth),
                  postCode.ToString(),
                  Convert.ToDateTime(appointmentTime),
                  Convert.ToInt32(resultType))
                  select person;
        {
            p.Forename,
            p.Surname,
            p.AppointmentTime,
            p.Location
        };

        foreach (var record in person)
        {
            lblName.Text = record.Forename + " " + record.Surname;
            lblAppointmentTime.Text = record.AppointmentTime.ToString();
            lblWaitIn.Text = record.Location;
        }   

我们将不胜感激任何有关如何解决此问题的帮助。

更新 - 嗨,这是我的 SP:

@sex varchar (1),
@day int,
@month int,
@year int,
@PostCode varchar (10),
@AppTime DateTime,
@filter int

AS
BEGIN

declare @sql nvarchar(max)
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here

Select @Sql = 'SELECT DAY(DateOfBirth) AS DayOfBirth, MONTH(DateOfBirth) AS MonthOfBirth, 
YEAR(DateOfBirth) AS YearOfBirth, DateOfBirth, Sex, AppointmentTime, SchdlRefno, Forename, 
Surname, ClinicCode, Ur, PostCode, Specialty, Location
FROM dbo.tbl_Appointments'


if @filter  = 1
-- show search by just sex, day and month as no more needed for match
Select @sql = @Sql + ' WHERE (DAY(DateOfBirth) = ' + convert(varchar, @day, 103) + ') AND (MONTH(DateOfBirth) = ' + convert(varchar, @month, 103) + ') AND (Sex = ''' + @sex + ''')' 

if @filter  = 2

-- show search by sex, day and month and postcode

Select @sql = @Sql + ' WHERE (DAY(DateOfBirth) = ' + convert(varchar, @day, 103) + ') AND (MONTH(DateOfBirth) = ' + convert(varchar, @month, 103) + ') and (YEAR(DateOfBirth) = ' + convert(varchar, @year, 103) + ') AND (Sex = ''' + @sex + ''') and (postcode = ''' + @postcode + ''')' 

if @filter  = 3

-- show search by sex, day and month, postcode and appointment time

Select @sql = @Sql + ' WHERE (DAY(DateOfBirth) = ' + convert(varchar, @day, 103) + ') AND (MONTH(DateOfBirth) = ' + convert(varchar, @month, 103) + ') and (YEAR(DateOfBirth) = ' + convert(varchar, @year, 103) + ') AND (Sex = ''' + @sex + ''') and (postcode = ''' + @postcode + ''') and (AppointmentTime = ''' + convert(varchar, @AppTime, 121) + ''')' 

print @sql  

Exec sp_executesql  @sql

END

所以,我通过一个过滤器来返回不同的搜索结果。

谢谢

【问题讨论】:

    标签: c# asp.net linq linq-to-sql int


    【解决方案1】:

    问题是您的函数的结果不是集合,而是标量值 (int),因此无法投影。

    【讨论】:

    • 感谢您的快速回复。但是我该如何去改变它,因为它不是我以前遇到过的?
    • @Adamsk1 您需要更新您的 SP 以返回实际查询结果而不是整数值。您能否发布您的 SP 的实施细节?
    • @Adamsk1 我会更新您的问题以显示您的 SP 的实施。
    • 看起来您正在使用动态 SQL - 确保您在 SQL Injection 上有所了解。您的查询看起来没问题(假设这在设计器中返回值没问题?)。我认为使用动态 SQL,L2S 生成器很难找到返回类型,因此您可以尝试将查询结果转储到临时表中,并在执行 SQL 后让您的 SP 从中执行SELECT
    • 我认为临时表和在 DBML 上重新生成存储过程应该可以工作。我能想到的唯一其他方法是修改 DBML XML 以强制返回类型......我认为临时表方式更可靠。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多