Search code examples
c#entity-frameworklinq-to-entities

how to sort a list of object value in an object


This is the query that i use to fetch items and item detail of each item with specific 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;
}

To make the requirement clear there are two table ItemTable and ItemDetailTable And Id being ItemTable primary key and foreign key in ItamDetailTableand the ItemDetailTable may have multiple values for a single ID and the above query returns the item and the details from ItemDetailTable with the specific id what i want is to have a sorted item detail value by the value of Amount. Is there any way to do that ?

Sample Data

ItemTable

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

what i want is when the value of Id is 1 the return value to be

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
    }
] 

Solution

  • You need to return sorted list of ItemModel by passing id parameter to GetItems method.

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

    Edit:

    the above query returns the item and the details from ItemDetailTable with the specific id

    You also need to add foreign key property to ItemDetailTable like

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

    Edit 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;
    }