列表<对象>排序在Asp.net c#中使用linq进行升序和降序排序
本文关键字:排序 linq 升序 降序 对象 Asp 列表 net | 更新日期: 2023-09-27 18:01:48
大家好,我正在从web 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();