Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 625 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use orderby with 2 fields in linq?

#1
Say I have these values in a database table

id = 1
StartDate = 1/3/2010
EndDate = 1/3/2010

id = 2
StartDate = 1/3/2010
EndDate = 1/9/2010

Now I have so far this orderby for my linq

var hold = MyList.OrderBy(x => x.StartDate).ToList();

I want to order it however also using the end date.

Like so the order I would want this in as

id 2
id 1

So `endDates` that are greater go first. I am not sure if I need to change this to use some compare function or something.
Reply

#2
MyList.OrderBy(x => x.StartDate).ThenByDescending(x => x.EndDate);
Reply

#3
Use [`ThenByDescending`](

[To see links please register here]

):

var hold = MyList.OrderBy(x => x.StartDate)
.ThenByDescending(x => x.EndDate)
.ToList();

You can also use query syntax and say:

var hold = (from x in MyList
orderby x.StartDate, x.EndDate descending
select x).ToList();

`ThenByDescending` is an extension method on [`IOrderedEnumerable`][2] which is what is returned by [`OrderBy`](

[To see links please register here]

). See also the related method [`ThenBy`](

[To see links please register here]

).

[2]:

[To see links please register here]

Reply

#4
VB.NET

MyList.OrderBy(Function(f) f.StartDate).ThenByDescending(Function(f) f.EndDate)

OR



From l In MyList Order By l.StartDate Ascending, l.EndDate Descending
Reply

#5

MyList.OrderBy(x => x.StartDate).ThenByDescending(x => x.EndDate);

Note that you can use as well the Descending keyword in the OrderBy (in case you need). So another possible answer is:

MyList.OrderByDescending(x => x.StartDate).ThenByDescending(x => x.EndDate);
Reply

#6
If you have two or more field to order try this:

var soterdList = initialList.OrderBy(x => x.Priority).
ThenBy(x => x.ArrivalDate).
ThenBy(x => x.ShipDate);


You can add other fields with clasole "ThenBy"
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through