views:

321

answers:

1

i make a group by and get child lists... during query i create a new obj with

var result3 = from tick in listTicks
              group tick by bla bla into g
              select new 
              { 
                  Count = g.Count(), 
                  Key = g.Key, 
                  Items = g,
                  Timestamp = g.First().timestamp,
                  LastTimestamp = g[-1].First().timestamp result3 isn't still declared???
              };

i want have access during runtime in the select new on values of the last created obj maybe check if the last first.Timestamp has a specific value

is it possible to have access to the last g during creating the select new { } i want to check an actual value withe one from the last g

i thought something like result3[result.count - 1].timestamp??? in the select new part...

+1  A: 

Not should I understand correctly, but is this what you want ?

result3.Last().Timestamp;


After comment : I think I understand now. You need to create a temporary variable to store the timestamp of the last group and set its value in a more complex delegate :

int lastTimestamp = 0; // Put the correct type and default value

var result3 = (from tick in listTicks
              group tick by bla bla into g
              select g)
              .Select
              (g => 
              {
                  // Create your object with the last timestamp
                  var result = new
                  { 
                      Count = g.Count(), 
                      Key = g.Key, 
                      Items = g,
                      Timestamp = g.First().timestamp,
                      LastTimestamp = lastTimestamp
                  };
                  // Set last timestamp for next iteration
                  lastTimestamp = result.Timestamp;
                  // Return your object
                  return result;
              });

Don't know the exact context, but you might want to add "ToList()" to override lazy fetching.

ybo
this doesn't work... the code looks good... but in debug... the result3 does'nt have the new variable from temp result Counte, Key .... etc...
the fault is result.Timestamp -> has to be g...