linq为存储过程的两次不同执行返回相同的数据?

linq为存储过程的两次不同执行返回相同的数据?,linq,entity-framework,linq-to-entities,Linq,Entity Framework,Linq To Entities,我有一个通过实体框架调用的存储过程 存储过程有2个日期参数。我在调用存储过程的两次过程中提供了不同的参数。我已经使用SQL事件探查器验证了存储过程是否被正确调用并返回了正确的结果 当我第二次使用不同的参数调用我的方法时,即使存储过程返回正确的结果,创建的表包含与我第一次调用它时相同的数据 dtStart = 01/08/2009 dtEnd = 31/08/2009 public List<dataRecord> GetData(DateTime dtStart, DateTi

我有一个通过实体框架调用的存储过程

存储过程有2个日期参数。我在调用存储过程的两次过程中提供了不同的参数。我已经使用SQL事件探查器验证了存储过程是否被正确调用并返回了正确的结果

当我第二次使用不同的参数调用我的方法时,即使存储过程返回正确的结果,创建的表包含与我第一次调用它时相同的数据

dtStart = 01/08/2009  
dtEnd = 31/08/2009

public List<dataRecord> GetData(DateTime dtStart, DateTime dtEnd)
{
  var tbl = from t in db.SP(dtStart, dtEnd)                       
                      select t;
  return tbl.ToList();            
}

GetData((new DateTime(2009, 8, 1), new DateTime(2009, 8, 31))
// tbl.field1 value = 45450 - CORRECT

GetData(new DateTime(2009, 7, 1), new DateTime(2009, 7, 31))
// tbl.field1 value = 45450 - WRONG 27456 expected 

考虑将DataService类更改为如下所示:

public partial class DataService
{
    public List<MeterTotalConsumpRecord> 
                      GetTotalAllTimesConsumption(SearchCriteria sc)
    {    
        var db = new dbChildDataContext(); // new every time, just in this test case.

        var totalConsumption = db.GetTotalConsumptionByMeter(sc.DtStart, 
                                                             sc.DtEnd, 
                                                             sc.ug, 
                                                             sc.MeterSelectionType, 
                                                             sc.CustomerID, 
                                                             sc.UserID, 
                                                             sc.Selection, 
                                                             sc.ClosedLocations, 
                                                             sc.DisposedLocations, 1)
                                 .ToList();                       

        //inspect how many results are returned.
        int rowCount = totalConsumption.Count;


        return totalConsumption;

    }
} 


//use objects to pass between classes
public class SearchCriteria
{
    public DateTime DtStart {get;set;}
    public DateTime DtEnd {get;set;}
    public int ug {get;set;}
    public int MeterSelectionType {get;set;}
    public int CustomerID {get;set;}
    public int UserID {get;set;}
    public string Selection {get;set;}
    public bool ClosedLocations {get;set;}
    public bool DisposedLocations  {get;set;}
}
公共部分类数据服务
{
公开名单
GetTotallTimesConsumption(搜索条件sc)
{    
var db=new dbChildDataContext();//每次都是新的,就在这个测试用例中。
var总消耗=db.GetTotalConsumptionByMeter(sc.DtStart,
sc.DtEnd,
sc.ug,
sc.MeterSelection类型,
sc.CustomerID,
sc.UserID,
sc.选择,
sc.ClosedLocations,
sc.处置地点,1)
.ToList();
//检查返回的结果数量。
int rowCount=totalConsumption.Count;
返回总消耗量;
}
} 
//使用对象在类之间传递
公共类搜索标准
{
公共日期时间DtStart{get;set;}
公共日期时间DtEnd{get;set;}
公共int ug{get;set;}
public int meterselection类型{get;set;}
public int CustomerID{get;set;}
public int UserID{get;set;}
公共字符串选择{get;set;}
公共bool ClosedLocations{get;set;}
公共布尔配置位置{get;set;}
}

我已经尽了最大努力编辑了这篇文章,但老实说,我很难弄清楚这里到底发生了什么。您可能想发布一个实际的工作代码示例,而不是在这里或那里发布一些零碎的代码。谢谢您对其进行了很好的编辑!当我为2009年8月打电话给GetData时,我得到了我期望的号码——45450。当我为2009年7月打电话给GetData时,我也得到了45450。这是错误的,我希望得到27456我偶尔得到的确切错误消息是System.InvalidOperationException:查询结果不能枚举多次。不确定这是否有什么关系?保罗,那没用,你只是重复了你已经给我们的信息。首先,
GetData
会返回一个列表,而您会告诉我们单个记录的结果,这毫无意义。这里没有足够的代码来跟踪错误消息的来源——当您尝试重用查询结果时,就会发生这种情况。如前所述,请发布一个完整的代码示例,以再现您的问题。“db”变量是静态成员范围变量吗?我相信是的,我们刚刚添加了一个完整的代码列表谢谢,这是可行的,但没有办法避免创建新的上下文吗?
public partial class DataService
{
    public List<MeterTotalConsumpRecord> 
                      GetTotalAllTimesConsumption(SearchCriteria sc)
    {    
        var db = new dbChildDataContext(); // new every time, just in this test case.

        var totalConsumption = db.GetTotalConsumptionByMeter(sc.DtStart, 
                                                             sc.DtEnd, 
                                                             sc.ug, 
                                                             sc.MeterSelectionType, 
                                                             sc.CustomerID, 
                                                             sc.UserID, 
                                                             sc.Selection, 
                                                             sc.ClosedLocations, 
                                                             sc.DisposedLocations, 1)
                                 .ToList();                       

        //inspect how many results are returned.
        int rowCount = totalConsumption.Count;


        return totalConsumption;

    }
} 


//use objects to pass between classes
public class SearchCriteria
{
    public DateTime DtStart {get;set;}
    public DateTime DtEnd {get;set;}
    public int ug {get;set;}
    public int MeterSelectionType {get;set;}
    public int CustomerID {get;set;}
    public int UserID {get;set;}
    public string Selection {get;set;}
    public bool ClosedLocations {get;set;}
    public bool DisposedLocations  {get;set;}
}