0

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;
    }
4
  • can you explain these two sentences a bit more "So it can happen when I send it empty. I want it to fetch with "like" when I send it blank." i dont understand what you mean here Commented Mar 20, 2023 at 12:43
  • My english is bad, sorry. Let me say this briefly. I am sending companyId, userName, page, pageSize parameters. Sometimes when I send "username, page, pageSize", no data is coming, so it doesn't search. Commented Mar 20, 2023 at 12:56
  • stackoverflow.com/questions/71641741/… I want to do a filter like here Commented Mar 20, 2023 at 13:30
  • Use if-else conditions to generate your response query. if(string.IsNotNullOrEmpty(userName){ // use LIKE} else { // something else } Commented Mar 20, 2023 at 14:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.