使用Asp.net中的linq以升序和降序对列表<object>进行排序c#

时间:2016-08-11 09:38:30

标签: c# .net list generic-list

大家好,我正在从web api中获取并将其存储在var&#34; obj1&#34;中。然后将其添加到列表&#34; snap&#34;对象类型......我必须对&#34; snap&#34;执行操作所以列表应该按照一个参数升序排列,例如&#34;距离&#34; ....请帮帮我

这是我的代码..

List<object> snap = new List<object>();
var obj1 = objEntity.TB_Broadcast_Snap.Where(s => s.snapid == sid)
                                      .Join(objEntity.TB_User_Registration,
                                            u => u.userid,
                                            v => v.userid,
                                            (u, v) => new { TB_Broadcast_Snap = u, TB_User_Registration = v })
                                      .Select(s => new
                                      {
                                          s.TB_Broadcast_Snap.snapid,
                                          s.TB_Broadcast_Snap.title,
                                          s.TB_Broadcast_Snap.details,
                                          s.TB_Broadcast_Snap.category,
                                          s.TB_Broadcast_Snap.tag,

                                          s.TB_Broadcast_Snap.broadcastdistance,
                                          s.TB_Broadcast_Snap.images,
                                          s.TB_Broadcast_Snap.latitude,
                                          s.TB_Broadcast_Snap.longitude,
                                          s.TB_Broadcast_Snap.image_detail1,
                                          s.TB_Broadcast_Snap.image_detail2,
                                          s.TB_Broadcast_Snap.image_detail3,
                                          s.TB_Broadcast_Snap.barcode_image,
                                          s.TB_Broadcast_Snap.broadcast_date,
                                          s.TB_Broadcast_Snap.promocode,
                                          snaptitle = s.TB_Broadcast_Snap.title,
                                          s.TB_Broadcast_Snap.broadcasttype,
                                          s.TB_Broadcast_Snap.regionname,
                                          distance = 0,
                                          popcountno = popcount,
                                          issnaped = issnapped,
                                          //Vendor Detail
                                          s.TB_User_Registration.emailid,
                                          address1 = "No address",

                                          s.TB_User_Registration.userid,
                                          s.TB_User_Registration.username,
                                          s.TB_User_Registration.imageurl,
                                          s.TB_User_Registration.business_name,
                                          s.TB_User_Registration.vendortype,
                                          vendorname = s.TB_User_Registration.name,
                                      }).ToList();
snap.AddRange(obj1);

1 个答案:

答案 0 :(得分:1)

这是首先按Distance订购,然后按另一个字段降序的方式:

var obj1 = objEntity.TB_Broadcast_Snap.Where(s => s.snapid == sid)
                                       .Join(/*code*/)
                                       .Select(/*code*/)
                                       .OrderBy(item => item.Distance)
                                       .ThenByDescending(item => item.OtherField)
                                       .ToList();