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

Sorting of list<object> in ascending and descending order using linq in Asp.net c#

大家好,我正在从网络 api 获取响应并将其存储在 var "obj1" 上。然后将其添加到对象类型的列表 "snap" 中......我必须对 "snap" 执行操作,以便该列表应根据 "distance" 等参数按升序排列......请帮助我

这是我的代码..

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

这是先按 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();