Sayli Vaidya Sayli Vaidya - 7 months ago 35
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?


Try this:-

var result= SharedStories.OrderByDescending(x => x.SharedOnFacebookCount 
                                               + x.SharedOnGoogleCount 
                                               + x.SharedOnTwitterCount 
                                               + x.SharedOnInstagramCount 
                                               + x.SharedOnLinkedInCount 
                                               + x.SharedOnPinterestCount)