How to Access more than One table from a single controller in ASP.Net core web api without using MVC

  • Thread starter Thread starter Swarnava Sen
  • Start date Start date
S

Swarnava Sen

Guest
I have one Controller Named as Info.
which contains the following method for sorting data for ascending and descending, price greater than less than and like operation from Only table Named Products.


[HttpGet]
public IEnumerable<Products> GetData([FromQuery]Products sm, [FromQuery]int skip, [FromQuery]int take, [FromQuery]string sortColName,
[FromQuery]string sortdirection, [FromQuery(Name = "price[gt]")] float price1, [FromQuery(Name = "price[lt]")] float price2,
[FromQuery(Name="colour[like]")] string clr)
{
var data = _context.Products.ToList();
var sorting = (from p in data select p);
var dataOrder = _context.Orders.ToList();
var sorting1 = (from o in dataOrder select o);
List<Products> TakeResult = null;
List<Products> ResultOfSort = null;
List<Products> ResultOfSkip = null;
List<Products> ResultOfPrice = null;

// Sorting Part
if (sortColName != null && sortdirection != null)
{
TakeResult = sortMethod(data, sortColName, sortdirection);
}
if (skip != 0 || take != 0 && TakeResult!=null)
{
ResultOfSort = SkipMethod(TakeResult, skip, take);
}
if(price1 != 0 || price2 != 0 && ResultOfSort!=null)
{
ResultOfSkip = PriceMethod(ResultOfSort, price1, price2);
}
if (clr != null && ResultOfSkip!=null)
{
ResultOfPrice = ColourMethod(ResultOfSkip, clr);
return ResultOfPrice;
}
//Main method return
return sorting;

}

// All Methods
//Sorting
private List<Products> sortMethod(List<Products>data,string sortColName, string sortdirection)
{
var sorting = (from p in data
select p).ToList();
var stcolName = typeof(Products).GetProperty(sortColName);
List<Products> sortingRes = null;
switch (sortdirection)
{
case "desc":
sortingRes = sorting.OrderByDescending(o=>stcolName.GetValue(o)).ToList();
break;

default:
sortingRes = sorting.OrderBy(o => stcolName.GetValue(o)).ToList();
break;

}
return sortingRes;
}

//Skip and Take Method
private List<Products> SkipMethod(List<Products> TakeResult, int skip, int take)
{
var data = _context.Products.ToList();
var result = (from s in TakeResult.Skip(skip).Take(take) select s).ToList();
return result;
}

//Price
private List<Products> PriceMethod(List<Products> ResultOfSort, float price1, float price2)
{
List<Products> priceresult = null;
if (price1 != 0)
{
priceresult = ResultOfSort.Where(o => o.Price > price1).ToList();
}
else if (price2 != 0)
{
priceresult = ResultOfSort.Where(o => o.Price < price2).ToList();
}
return priceresult;
}

//Colour Method
private List<Products> ColourMethod(List<Products> ResultOfSkip, string clr)
{
List<Products> resClr = null;
resClr = ResultOfSkip.Where(a => a.Colour == clr).ToList();
return resClr;
}


Now I wanted to access another table Named Order from the same web api and using same method for both tables.
Please help me out.


Model Class are like


public class Products
{
public int Id { get; set; }
public string Name { get; set; }
public String Category { get; set; }
public float Price { get; set; }
public string Brand { get; set; }
public String Colour { get; set; }
public string Size { get; set; }
public float Weight { get; set; }

}
public class Order
{
public int Id { get; set; }
public String OrderName { get; set; }
public string OrderCategory { get; set; }
public float Price { get; set; }
public string Brand { get; set; }
public string Colour { get; set; }
public string Size { get; set; }
public float Weight { get; set; }


}
public class PDetailsContext : DbContext
{
public PDetailsContext()
{
}

public PDetailsContext(DbContextOptions<PDetailsContext> options) :
base(options)
{
}
public DbSet<Products> Products { get; set; }
public DbSet<Order> Orders { get; set; }

}

Continue reading...
 
Back
Top