views:

438

answers:

3
public void metodoX()
{ 
 foreach (TURNO t in listaTurnoPersona)
 {
  DataServiceQuery<VST_CANTIDAD_PERSONAS_POR_DIA> query = 
    General.Entities.VST_CANTIDAD_PERSONAS_POR_DIA.Where(
                    z => z.ID_TURN == t.ID_TURN 
                        && z.FE_CALE >= RadDatePicker1.SelectedDate.Value
                        && z.FE_CALE <= RadDatePicker1.SelectedDate.Value.AddDays(6)) 
    as DataServiceQuery<VST_CANTIDAD_PERSONAS_POR_DIA>;
  query.BeginExecute(ProcesarHorarioPersonasTurno, query);
  //HERE THE ID_TURN CHANGE 1, 2, 3 , 4 ...
 }
}

public void ProcesarHorarioPersonasTurno(IAsyncResult result)
{
            List<VST_CANTIDAD_PERSONAS_POR_DIA> listaDias = (result.AsyncState as   DataServiceQuery<VST_CANTIDAD_PERSONAS_POR_DIA>).EndExecute(result).ToList();
         //HERE ALWAYS I GET THE RESULT IDTURN = 1}


Please Check the code above, why is happening this, is supposed that the ID_TURN also change, this doesn't was happening before.

A: 

You're capturing the iterator variable 't' in your invocation of Where(), which is a lambda expression:

Where( z => z.ID_TURN == t.ID_TURN && z.FE_CALE ...

By the time the queries execute, they're all referencing the same 't', which in your case is the final value before the loop terminates. In other words, they are all getting the same value for t.ID_TURN, which in your example is '1'.

To do this properly, declare a new variable:

var id = t.ID_TURN;

DataServiceQuery query = General.Entities.VST_CANTIDAD_PERSONAS_POR_DIA
    .Where( z => z.ID_TURN == id && 
        z.FE_CALE >= RadDatePicker1.SelectedDate.Value && 
        z.FE_CALE <= RadDatePicker1.SelectedDate.Value.AddDays(6)) 
    as DataServiceQuery;

This will capture a different variable for each invocation of Where, which will get the results you want.

Ben M
A: 

With this, now the result.AsyncState URL is changing the ID_TURN value (before i only was getting the last one always(128M))

{http://localhost:888/Services/WebDataServiceSiata.svc/VST_CANTIDAD_PERSONAS_POR_DIA()?$filter=((**ID_TURN eq 21M**) and (FE_CALE ge datetime'2009-07-20T00:00:00-05:00')) and (FE_CALE le datetime'2009-07-26T00:00:00-05:00')}

But the EndExecuteResult is still returning listaDia[0].ID_TURN = 1

List listaDias = (result.AsyncState as DataServiceQuery).EndExecute(result).ToList()
Dennis, edit your question, don't add answers. Thanks.
John Saunders
IM sorry, the browser closed and because im not registered i cant edit the first question.
Ok .. but your handler (ProcesarHorarioPersonasTurno) is being called four times, yes? You're not expecting the first handler call to contain all the results, right? (Had to ask -- sorry if this is obvious.)
Ben M
actually is 128 times, in the 128 time the ProcesarHorarioPersonasTurno method is accessed the ID_TURN is the same, i can see that result.asyncstate query is changing now, but the endexecute is returning the same result.
Sorry, Dennis, I'm at a loss. If I think of anything later I'll add a new comment. In the meantime, consider registering... it makes things much easier, and more people are more likely to answer your questions. :-)
Ben M
+1  A: 

i Found that the entity returned VST_CANTIDAD_PERSONAS_POR_DIA is just being created only once!(when the the method is called for first time); i think this maybe is because the model is in another project or because im using Entity framework extensions.

BTW im registered now, sorry thtat i add an answer but i can comment or edit the another post.

Vanilla
Thanks for coming back!
John Saunders
The problem was that i created the Entities globally, now im instancing a new Entity inside the foreach and is working finally.
Vanilla