简体   繁体   中英

Order a List<T> by Date, but it has a String ID, if that ID is repeated with different date in the collection with should appear below of it

I couldn't express better what I am asking in the title.

This is what I'm looking.

I have a disordered List of an Specific Object I have a DateTime and String Property.

The String Property Has values Like this ones (note that it is an string, not a number, it always has the K letter, I should be ordering with just the numbers):

K07000564, 
K07070000
K07069914
K07026318
K07019189

What I want is to order the List By Date... but when ordering if the String value is present in the collection with other Date I want to order them just after this one (By Date also in that miniGroup of IdFinders)... and then keep ordering...

Something Like this:

Edit

I edited the example to clarify that ordering by IdFinder will not work... I need to order By Date.. if when ordering by Date the IdFinder is present more than once in the collection should show them just after this last one, and then keep ordering the rest of them and so on by each idfinder

ID         Date
**K07000564**   Today
K07000562   Yesterday
K07000563   The Day Before Yesterday
**K07000564** The day before the day before yesterday

Should be

K07000564 Today
K07000564 The day before the day before yesterday
K07000562 Yesterday 
K07000563  The Day Before Yesterday 

I achieved this in SQL Server 2008 in a project before with something like this:

WITH B
AS
(
    SELECT 
        ID, 
        MAX(DATE_COLUMN) DATE_COLUMN, 
        ROW_NUMBER() OVER (ORDER BY MAX(DATE_COLUMN) DESC) RN
    FROM MYTABLE
    GROUP BY ID

)

SELECT *
FROM MYTABLE c
, B
WHERE ID= b.ID
ORDER BY b.rn, c.DATE_COLUMN desc;

But I'm not good with Linq and I have no idea of how doing this in Linq.

Maybe an Important Note I'm in .NEt 2.0, so no LINQ available but I'm using Linqbridge to use Linq.

I tried this, but as you will notice, this will not work

oList.OrderBy(i => i.IdFinder).ThenByDescending(i => i.OperationDate);

I hope to have explained this clearly

var result = oList.OrderByDescending(x => x.OperationDate)
                  .GroupBy(x => x.IdFinder)
                  .SelectMany(x => x);

I think this should do the trick:

var sortedList = oList
    .GroupBy(x => x.IdFinder)
    .Select(g =>
        new
            {
                MaxOpDate = g.Max(x => x.OperationDate),
                Items = g
            })
    .OrderByDescending(g => g.MaxOpDate)
    .SelectMany(g => g.Items.OrderByDescending(x => x.OperationDate));

However, I haven't tested it with Linqbridge.

Try this

oList.OrderBy(i => int.parse(i.IdFinder.Substring(1,i.IdFinder.Length-1)).ThenByDescending(i => i.OperationDate);

First extract the numeric value out of IdFinder, the orderby this value.

Note -> It is assumed that i.IdFinder is always a valid "K######" where # is number less than Int32.MaxValue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM