SQL SP sp_executesql只需将int返回到实体框架即可

时间:2017-08-23 09:40:06

标签: c# sql sql-server stored-procedures

这是我的SP代码的一部分:

set @strSql = 'select Count(id) as CountId, FirstRouteNo,ThroughRouteSid,LastRouteNo,'
        set @strSql +='(select top 1 ThroughRouteJson from'+@tableName+'as subQuery where mainQuery.FirstRouteNo=subQuery.FirstRouteNo and mainQuery.ThroughRouteSid = subQuery.ThroughRouteSid and mainQuery.LastRouteNo = subQuery.LastRouteNo) as DetailJson,'+' '
        set @strSql +='(select top 1 RouteMeter from'+@tableName+'as subQuery where mainQuery.FirstRouteNo=subQuery.FirstRouteNo and mainQuery.ThroughRouteSid = subQuery.ThroughRouteSid and mainQuery.LastRouteNo = subQuery.LastRouteNo) as RouteMeter'+' '
        set @strSql +='from'+@tableName+'as mainQuery where [OperateDate] >='+QUOTENAME(CONVERT(char(10),@startMonth,20),'''')+' and [OperateDate] <'+QUOTENAME(CONVERT(char(10),@endMonth,20),'''')+' and'+' '
        set @strSql +='[FirstAbsoluteBoardStopId] in '+@StringStartVillageStopSid+' and [LastAbsoluteAlightStopId] in '+@StringEndVillageStopSid+' '
        set @strSql +='group by mainQuery.[FirstRouteNo],mainQuery.[ThroughRouteSid],mainQuery.[LastRouteNo]'+' '
        set @strSql +='order by CountId desc'           
        execute sp_executesql  @StrSql

这是我在Visual Studio中的代码部分。只需返回int

   public virtual int GetVillageTransferData(Nullable<System.DateTime> startMonth, Nullable<System.DateTime> endMonth, string startTime, string endTime, string dateString, string analysisType, string isAllDay, string startVillageName, string endVillageName, Nullable<int> transferCount)
    {
        var startMonthParameter = startMonth.HasValue ?
            new ObjectParameter("startMonth", startMonth) :
            new ObjectParameter("startMonth", typeof(System.DateTime));

        var endMonthParameter = endMonth.HasValue ?
            new ObjectParameter("endMonth", endMonth) :
            new ObjectParameter("endMonth", typeof(System.DateTime));

        var startTimeParameter = startTime != null ?
            new ObjectParameter("startTime", startTime) :
            new ObjectParameter("startTime", typeof(string));

        var endTimeParameter = endTime != null ?
            new ObjectParameter("endTime", endTime) :
            new ObjectParameter("endTime", typeof(string));

        var dateStringParameter = dateString != null ?
            new ObjectParameter("dateString", dateString) :
            new ObjectParameter("dateString", typeof(string));

        var analysisTypeParameter = analysisType != null ?
            new ObjectParameter("analysisType", analysisType) :
            new ObjectParameter("analysisType", typeof(string));

        var isAllDayParameter = isAllDay != null ?
            new ObjectParameter("IsAllDay", isAllDay) :
            new ObjectParameter("IsAllDay", typeof(string));

        var startVillageNameParameter = startVillageName != null ?
            new ObjectParameter("startVillageName", startVillageName) :
            new ObjectParameter("startVillageName", typeof(string));

        var endVillageNameParameter = endVillageName != null ?
            new ObjectParameter("endVillageName", endVillageName) :
            new ObjectParameter("endVillageName", typeof(string));

        var transferCountParameter = transferCount.HasValue ?
            new ObjectParameter("transferCount", transferCount) :
            new ObjectParameter("transferCount", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("GetVillageTransferData", startMonthParameter, endMonthParameter, startTimeParameter, endTimeParameter, dateStringParameter, analysisTypeParameter, isAllDayParameter, startVillageNameParameter, endVillageNameParameter, transferCountParameter);
    }
}
  

这是我的SQL Management查询结果。我希望得到相同的结果   我的Visual Studio

It's my SQL Management query result.I want to get the same result in My Visual Studio 这是我的SQL SP代码的一部分。我想返回所有数据 是我的查询结果。我可以在我的SQL中看到所有结果数据 管理工作室。但我只是看到SQL Management Studio返回int 我的实体框架。我想问一下如何修复它以获取所有我的 查询数据?感谢

1 个答案:

答案 0 :(得分:0)

您需要使用.ExecuteFunction<T>()代替.ExecuteFunction()。 请参阅this link

创建一个代表结果的结果类,例如

public class VillageTransferResult {
    public int CountId { get; set; }
    public int FirstRouteNo { get; set; }
    ...
}

更改

public virtual int GetVillageTransferData(...)

public virtual ObjectResult<VillageTranferResult> GetVillageTransferData(...)

最后,使用

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<VillageTransferResult>("GetVillageTransferData", ...);

而不是

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("GetVillageTransferData", ...);