订单列表使用其字段的总和

时间:2014-11-14 05:47:30

标签: c# linq list

我正在开发一个MVC应用程序,我希望通过使用其字段值的总和来降序列表。我得到了这笔钱,但它并没有按照这笔总和的顺序下降。

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();

totlaSharedCountOfStory是我的字段总和,我想按totlaSharedCountOfStory的降序排列。任何解决方案?

2 个答案:

答案 0 :(得分:0)

选择SUM()为::

后,需要使用order by
SharedStories = (from p in PublishedLivestories.ToList()
                    select new{
                    totlaSharedCountOfStory = p.Sum(s => s.SharedOnFacebookCount
                                                                   + s.SharedOnGoogleCount
                                                                   + s.SharedOnInstagramCount
                                                                   + s.SharedOnLinkedInCount
                                                                   + s.SharedOnPinterestCount
                                                                   + s.SharedOnTwitterCount)
                   }).OrderByDescending(x=>x.totlaSharedCountOfStory).ToList();

答案 1 :(得分:0)

试试这个: -

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