In my api application, I send parameters via "controller" as follows.
For example; I am sending fields like "companyId", "userName" as parameters. But I don't always send "userName" field or "companyId" field full. So it can happen when I send it empty. I want it to fetch with "like" when I send it blank. can i do this?
public async Task<List<Inventory>> GetInventoryList(int companyId, string userName, int page, int pageSize)
{
IQueryable<Inventory> query;
query = _context.Inventories
.Where(x => x.CompanyId == companyId)
.OrderByDescending(x => x.CreatedDate);
int totalCount = query.Count();
var response = await query.Skip((pageSize * (page - 1)))
.Take(pageSize)
.Select(x => new Inventory()
{
CompanyId = x.CompanyId,
CreatedDate = x.CreatedDate,
Barcode = x.Barcode,
BrandId = x.BrandId,
BusinessCode = x.BusinessCode,
CategoryId = x.CategoryId,
CategorySubId = x.CategorySubId,
Id = x.Id,
Imei = x.Imei,
InventoryDate = x.InventoryDate,
InvoiceDate = x.InvoiceDate,
Mac = x.Mac,
ModelId = x.ModelId,
Name = x.Name,
SerialNumber = x.SerialNumber,
Status = x.Status,
Responsible = x.Responsible,
TotalCount = totalCount,
UpdatedDate = x.UpdatedDate
}).ToListAsync();
return response;
}
if-elseconditions to generate yourresponsequery.if(string.IsNotNullOrEmpty(userName){ // use LIKE} else { // something else }