none
Best way to get get next record RRS feed

  • Question

  • Hello,

    I am trying to get the Next record, I can do it with a loop (as seen below) but I was thinking that there must be a better way. So, my question is, is there a better way? (using c# mvc 3)

     

     var emps = from m in db.Employees
    			 where m.Updated != null
    			  select m;
    			  
    bool exitOnNext = false;
    string empNum = "";
    foreach (var result in emps)
    {
    	if (result.EmpNumber == employeeInformation.EmpNumber)
    	{
    		exitOnNext = true;
    		continue;
    	}
    	if (exitOnNext)
    	{                                
    		empNum = result.EmpNumber;
    		break;
    	}
    }
    

    Thanks

    Thursday, December 8, 2011 6:43 PM

Answers

  • Sorry - didn't know you were using LINQ to Entities.  Unfortunately, SkipWhile doesn't work there...

    You can do this, though:

    string nextEmpNumber = emps.AsEnumerable()
                                           .SkipWhile(emp => emp.EmpNumber != employeeInformation.EmpNumber)
                                           .Skip(1)
                                           .First().EmpNumber;
    
    



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by smithsf22 Thursday, December 8, 2011 8:56 PM
    Thursday, December 8, 2011 8:24 PM

All replies

  • You can use:

     

    var emps = from m in db.Employees
    			 where m.Updated != null
    			  select m;
    
    string nextEmpNumber = emps
                                           .SkipWhile(emp => emp.EmpNumber != employeeInformation.EmpNumber)
                                           .Skip(1)
                                           .First().EmpNumber;
    
    


     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 8, 2011 7:07 PM
  • Thanks,

    I looked at Skip before and I was never able to get it to work. The error I get when I try the above is:

    System.NotSupportedException: LINQ to Entities does not recognize the method
    'System.Linq.IQueryable`1[EmployeePortal.Models.EmployeeInformation] SkipWhile[EmployeeInformation](System.Linq.IQueryable`1[EmployeePortal.Models.EmployeeInformation],
    System.Linq.Expressions.Expression`1[System.Func`2[EmployeePortal.Models.EmployeeInformation,System.Boolean]])' method, and this method cannot be translated into a store expression.

     

    Any ideas?

    Thanks again

    Thursday, December 8, 2011 8:03 PM
  • Sorry - didn't know you were using LINQ to Entities.  Unfortunately, SkipWhile doesn't work there...

    You can do this, though:

    string nextEmpNumber = emps.AsEnumerable()
                                           .SkipWhile(emp => emp.EmpNumber != employeeInformation.EmpNumber)
                                           .Skip(1)
                                           .First().EmpNumber;
    
    



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by smithsf22 Thursday, December 8, 2011 8:56 PM
    Thursday, December 8, 2011 8:24 PM
  • Awesome!

    Thats way better than what I had.

    Thanks for taking the time, and sorry i didnt mention I was usin LINQ to Entities.

     

    Thursday, December 8, 2011 8:56 PM
  • Hi smithsf22,

    I'm glad to hear that you have resolved your issue. If you have any problems, please feel free to post in the forum.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Friday, December 9, 2011 2:08 AM
    Moderator