Improving algorithm and syntax for c# linq expression
I've been doing a bit of linq in c# .net 3.5 and feel like I should be able to do the following a little neater.
Is there a way to combine this into a single linq statement.
The conditions are as follows
- If the list contains an element where count is greater or equal to the threshold then we can use the list
- Order the list by count
- Order the list by date
I'd also like to add another condition on the count where I could treat all items with a count greater or equal than the threshold the same. I could do this by limiting the count to the threshold but i'd prefer not to in case the threshold were to be changed. I'm a bit stumped on how 开发者_如何学Goto do this other than temporarily editing the records when I get them from the database and not saving them. i.e with a threshold of 3 the list (1,2,3,4,5) becomes (3,3(4),3(5),2,1) before being sorted by date.
var allFaves = m_favouriteRepo.Get(user);
if(allFaves.Any(t => t.Count >= threshold))
{
var ordered = allFaves
.OrderByDescending(x => x.Count)
.ThenByDescending(x => x.SortDate)
.ToList();
}
Thanks Neil
if(allFaves.Any(t => t.Count >= threshold))
{
var ordered = allFaves
.OrderByDescending(x => x.Count>=threshold?threshold:x.Count)
.ThenByDescending(x => x.DatesHistory)
.ToList();
}
精彩评论