none
Multi parameter query RRS feed

  • Question

  • Hi

    I have table containing measurements like below:

    Row   Date                                  Addr   Channel_O  
    1        2011-02-27 15:21:58.830   A        1  
    2        2011-02-27 09:06:30.697   A        1                
    3        2011-02-26 22:59:57.060   A        0
    4        2011-02-26 22:59:53.160   A        0
    5        2011-02-26 20:48:32.720   A        1
    6        2011-02-27 15:21:24.890   B        1
    7        2011-02-27 07:42:31.300   B        1
    8        2011-02-26 22:10:25.943   B        0
    9        2011-02-26 20:48:32.933   B        1

    I want to get the newest measurement from Addr A and B in my output (here row number 1 and 6).

    I have tried this:

    var deviceCount = owdc.OneWireLevelDevices.Count();

    return owdc.OneWirelevelMeasurements.OrderByDescending(a => a.Addr).ThenByDescending(d => d.Date).Take(deviceCount);

    But this gives me row number 1 and 2.

    Can somebody help ?

    Thursday, June 7, 2012 5:49 PM

Answers

  • perhaps make 2 Linq queries, one that only takes rows for Addr='A' and one that take rows for Addr='B', then use the 'Max' operator to take the row with the highest date.

    something like this:

    var Results1 = owdc.OneWireLevelMeasurements.Where(p => p.Addr == 'A').Max(p => p.Date);
    
    var Results1 = owdc.OneWireLevelMeasurements.Where(p => p.Addr == 'B').Max(p => p.Date);

    then combine these results in an IEnumerable or List collection.

    • Marked as answer by Carsten 1 Monday, June 11, 2012 1:17 PM
    Friday, June 8, 2012 8:16 AM

All replies

  • perhaps make 2 Linq queries, one that only takes rows for Addr='A' and one that take rows for Addr='B', then use the 'Max' operator to take the row with the highest date.

    something like this:

    var Results1 = owdc.OneWireLevelMeasurements.Where(p => p.Addr == 'A').Max(p => p.Date);
    
    var Results1 = owdc.OneWireLevelMeasurements.Where(p => p.Addr == 'B').Max(p => p.Date);

    then combine these results in an IEnumerable or List collection.

    • Marked as answer by Carsten 1 Monday, June 11, 2012 1:17 PM
    Friday, June 8, 2012 8:16 AM
  • I combined the two IEnumerable this way:

    var combinedResult = Results1.Concat(Results2);

    Monday, June 11, 2012 1:25 PM