Sayli Vaidya Sayli Vaidya - 1 year ago 74
C# Question

order list by using sum of its fields

I am developing a MVC application and I want a list of items order by descending by using the sum of its field's valued. I got the sum but its not getting ordered by descending based on that sum.

var SharedStories = (from p in PublishedLivestories select p).ToList();

SharedStories = (from p in SharedStories
let totlaSharedCountOfStory = SharedStories.Sum(s => s.SharedOnFacebookCount
+ s.SharedOnGoogleCount
+ s.SharedOnInstagramCount
+ s.SharedOnLinkedInCount
+ s.SharedOnPinterestCount
+ s.SharedOnTwitterCount)
orderby totlaSharedCountOfStory descending
select p).ToList();

is my sum of fields and I want list in descending order by
. Any solution?

Answer Source

Try this:-

var result= SharedStories.OrderByDescending(x => x.SharedOnFacebookCount 
                                               + x.SharedOnGoogleCount 
                                               + x.SharedOnTwitterCount 
                                               + x.SharedOnInstagramCount 
                                               + x.SharedOnLinkedInCount 
                                               + x.SharedOnPinterestCount)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download