So I have this action on my app i'm building. It needs to search the database based on some criteria and return the list of items meeting the criteria
My database context looks like this
public DbSet<MarketCategory> MarketCategories { get; set; } //has .name
public DbSet<InventoryAsset> InventoryAssets { get; set; } //has .name
public DbSet<ItemCategory> ItemCategories { get; set; } // has .name
Each InventoryAsset has a column category and market that are related. So each item belongs to a category and each item also belongs to a market.
In my service I have the context in
private readonly InventoryContext _context;
I am trying to write this method but i'm a little naive with entity and linq
public IEnumerable<InventoryAsset> searchInventoryAssets(string query, string category, string market, string column)
{
return _context.//items that are part of the passed category, are also part of the passed market and where the passed column selected contains the passed query (such as InventoryAsset.ItemTitle == "red book")
}
The item model:
public string Title { get; set; }
public string Description { get; set; } // + more things like upc/brand/price etc
public virtual ItemCategory Category { get; set; }
public virtual MarketCategory Market { get; set; }
Both ItemCategory and MarketCategory are similar
public string Name {get; set;}
public virtual IEnumerable<InventoryAsset> CategoryAssets { get; set; }
If there's not like a single line of code I can write, I was going to so something like
var catItems = functionToReturnCatItems(category);
catItems.FirstOrDefault(a => a.Market.MarketPlaceName == market);
then do a if switch statement for each column in the db
if ( column == "Title"){ // search query for title...
}
Idk if this is a good solution though