0

Lets assume we have rows like this,

id       data1     data2     description    date
1          1         2           NULL        05/12/2014
2          3         3         Last Set      05/12/2014
3          1         2          Target       05/12/2014
4          0         0          Updated      05/12/2014
5          1         0       Revised Target  05/12/2014

Now what I want is to get latest out of the table by date first, but sometimes dates are same, so I need to get "Revised Target" but if there is no row for "Revised Target" then I need "Target" but if there is no row for target as well then I need "Target" but if there is no "Target" as well then I will take "Last Set".

So I am ignoring Updated and NULL ones here.

This is what I am trying but can't go any futther,

 var tar = (from m in table1 where m.Description != "Updated" &&  m.Description != NULL
                             select m).OrderByDescending(x => x.Date).FirstOrDefault();

Please note i only need 1 row based on the conditions i explained.

Edit

I tried answer with most votes below I am getting error on IndexOf method as it not takes 1 argument...

1
  • 2
    you might want to replace & with &&...
    – user57508
    Dec 5, 2014 at 9:01

3 Answers 3

4

So you're just missing the bit where you have some rules about which "description" to get, put these in an array in the order you want them

var descriptionOrder = new[]{"Revised Target","Target","Last Set"};

and use this to further order your results:

 var tar = (from m in table1 
            where m.Description != "Updated" &&  m.Description != null
            select m).OrderByDescending(x => x.Date)
            .ThenBy(x => Array.IndexOf(descriptionOrder,x.Description))
            .FirstOrDefault();
5
  • I was going to suggest using an Enum with a value, but this is much simpler.
    – toadflakz
    Dec 5, 2014 at 9:07
  • Thank you, but it says no overload method IndexOf takes 1 argument :( , even though i added using system.linq Dec 5, 2014 at 9:10
  • 1
    Try using List<T> then instead of an array. List<T>.IndexOf definitely only takes 1 argument.
    – toadflakz
    Dec 5, 2014 at 9:29
  • @CustomizedName - what .NET framework version are you targeting?
    – Jamiec
    Dec 5, 2014 at 9:57
  • @CustomizedName - Ok, I must be losing the plot - see update. You're right you cant call IndexOf like I did. Too much javascript ive forgotton my C#!
    – Jamiec
    Dec 5, 2014 at 10:11
2

Well, just add another Ordering

Think you'll need multiple ternary if you're in linq to entities, you may use a Dictionary or something like that to make things clearer if you're in linq to objects.

.OrderByDescending(x => x.Date)
.ThenBy(x => x.description == "RevisedTarget" 
             ? 0
             : (x.description == "Target" ? 1 :2)
 )
 .FirstOrDefault()
0

Case when ordering in Linq

 var tar = table1.Where(x=>x.description != null && x.description != "Updated")
            .OrderByDescending(x=>x.description == "Revised Target" ? 3 : 0)
            .ThenByDescending(x=>x.description == "Target" ? 2 : 0)
            .ThenByDescending(x=>x.description =="Last Set" ? 1 :0)
            .ThenByDescending(x=>x.date)
            .FirstOrDefault();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.