您需要返回已排序的
ItemModel
id
参数到
GetItems
方法。
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();
}
编辑:
上面的查询返回ItemDetailTable中具有特定id的项和详细信息
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;
}