如何对对象中的对象值列表进行排序
how to sort a list of object value in an object
这是我用来获取具有特定 ID 的项目和每个项目的项目详细信息的查询
public class ItemModel
{
public int ItemSeq{get;set}
public string ItemName{get;set;}
public double? Amount{get;set;}
}
public class SampleModel
{
public string Id{ get; set; }
public string Code{ get; set; }
public List<ItemModel> Items{ get; set; }
}
public List<SampleModel> GetItems(string id)
{
var items= _context.ItemTable.Where(t => t.Id== id).ToList<SampleModel>();
return list;
}
为了明确要求,有两个 table ItemTable
和 ItemDetailTable
并且 Id
是 ItemTable
中的主键和外键 ItamDetailTable
和 ItemDetailTable
可能有单个 ID
的多个值,上面的查询 returns 项目和来自 ItemDetailTable
的具有我想要的特定 ID 的详细信息按 Amount
的值排序项目详细信息值。有什么办法吗?
示例数据
项目表
Id Code
1 Code1
2 Code3
ItemDetailTable
Id ItemSeq ItemName Amount
1 1 ABC1 200
1 2 ABC2 129
1 3 ABC3 549
2 1 DEF1 265
2 2 DEf2 970
我想要的是当 Id
的值为 1 时 return 的值为
Id: 1
Code: Code 1
Items:[
0:{
ItemSeq: 2
ItemName: ABC2
Amount: 129
}
1:{
ItemSeq: 1
ItemName: ABC1
Amount: 200
},
1:{
ItemSeq: 3
ItemName: ABC3
Amount: 549
}
]
您需要通过将 id
参数传递给 GetItems
方法来 return 对 ItemModel
的列表进行排序。
public List<ItemModel> GetItems(string id)
{
var items = _context.SampleTable.Where(t => t.Id == id).SelectMany(x => x.Items).Where(p => p.Amount != null).OrderBy(x => x.Amount).ToList();
return items.ToList();
}
编辑:
the above query returns the item and the details from ItemDetailTable with the specific id
您还需要将外键 属性 添加到 ItemDetailTable 中,例如
public class ItemDetailTable
{
public int ItemSeq { get; set; }
public string ItemName { get; set; }
public double? Amount { get; set; }
public string Id { get; set; } //Foreign key of ItemTable
}
public ItemTable GetItems(string id)
{
//Get "ItemTable" record from database
var item = _context.ItemTable.Where(x => x.Id == id).SingleOrDefault();
//Retrieve its "Items" and sort by ascending order
var itemDetails = item.Items.Where(x => x.Id == id).Where(p => p.Amount != null).OrderBy(x => x.Amount).ToList();
//Preare a new "ItemTable" object to return
ItemTable itemTable = new ItemTable
{
Code = item.Code,
Id = item.Id,
Items = itemDetails
};
//Return new "itemTable" with sorted list of "ItemDetailTable"
return itemTable;
}
编辑 2:
public ItemTable GetItems(string id)
{
var result = (from i in _context.ItemTable
where i.Id == id
let sorting = i.Items.Where(x => x.Id == id).Where(x => x.Amount != null).OrderBy(x => x.Amount).ToList()
select new ItemTable
{
Id = i.Id,
Code = i.Code,
Items = sorting
}).FirstOrDefault(); //Or => SingleOrDefault
return result;
}
这是我用来获取具有特定 ID 的项目和每个项目的项目详细信息的查询
public class ItemModel
{
public int ItemSeq{get;set}
public string ItemName{get;set;}
public double? Amount{get;set;}
}
public class SampleModel
{
public string Id{ get; set; }
public string Code{ get; set; }
public List<ItemModel> Items{ get; set; }
}
public List<SampleModel> GetItems(string id)
{
var items= _context.ItemTable.Where(t => t.Id== id).ToList<SampleModel>();
return list;
}
为了明确要求,有两个 table ItemTable
和 ItemDetailTable
并且 Id
是 ItemTable
中的主键和外键 ItamDetailTable
和 ItemDetailTable
可能有单个 ID
的多个值,上面的查询 returns 项目和来自 ItemDetailTable
的具有我想要的特定 ID 的详细信息按 Amount
的值排序项目详细信息值。有什么办法吗?
示例数据
项目表
Id Code
1 Code1
2 Code3
ItemDetailTable
Id ItemSeq ItemName Amount
1 1 ABC1 200
1 2 ABC2 129
1 3 ABC3 549
2 1 DEF1 265
2 2 DEf2 970
我想要的是当 Id
的值为 1 时 return 的值为
Id: 1
Code: Code 1
Items:[
0:{
ItemSeq: 2
ItemName: ABC2
Amount: 129
}
1:{
ItemSeq: 1
ItemName: ABC1
Amount: 200
},
1:{
ItemSeq: 3
ItemName: ABC3
Amount: 549
}
]
您需要通过将 id
参数传递给 GetItems
方法来 return 对 ItemModel
的列表进行排序。
public List<ItemModel> GetItems(string id)
{
var items = _context.SampleTable.Where(t => t.Id == id).SelectMany(x => x.Items).Where(p => p.Amount != null).OrderBy(x => x.Amount).ToList();
return items.ToList();
}
编辑:
the above query returns the item and the details from ItemDetailTable with the specific id
您还需要将外键 属性 添加到 ItemDetailTable 中,例如
public class ItemDetailTable
{
public int ItemSeq { get; set; }
public string ItemName { get; set; }
public double? Amount { get; set; }
public string Id { get; set; } //Foreign key of ItemTable
}
public ItemTable GetItems(string id)
{
//Get "ItemTable" record from database
var item = _context.ItemTable.Where(x => x.Id == id).SingleOrDefault();
//Retrieve its "Items" and sort by ascending order
var itemDetails = item.Items.Where(x => x.Id == id).Where(p => p.Amount != null).OrderBy(x => x.Amount).ToList();
//Preare a new "ItemTable" object to return
ItemTable itemTable = new ItemTable
{
Code = item.Code,
Id = item.Id,
Items = itemDetails
};
//Return new "itemTable" with sorted list of "ItemDetailTable"
return itemTable;
}
编辑 2:
public ItemTable GetItems(string id)
{
var result = (from i in _context.ItemTable
where i.Id == id
let sorting = i.Items.Where(x => x.Id == id).Where(x => x.Amount != null).OrderBy(x => x.Amount).ToList()
select new ItemTable
{
Id = i.Id,
Code = i.Code,
Items = sorting
}).FirstOrDefault(); //Or => SingleOrDefault
return result;
}