Home > other >  c# data count query
c# data count query

Time:12-31

I have a Treatment List and their languages. When I send a LanguageId, if my incoming list is empty, I want to return 2 LanguageIds. Everything is fine, but how do I say if data=0?

 public async Task<ApiResponse<IEnumerable<TreatmentResponse>>> Handle(TreatmentsQuery request, CancellationToken cancellationToken)
    {
        IEnumerable<Treatment> data = await _repo.Treatment.GetByCurrencyIdAsync(request.CurrencyId, request.languageId, request.predicate);

        if (data==null)
        {
            var b = await _repo.Language.GetByIdAsync(a => a.isDefault == true);

            var c = request.languageId == b.Id;              
        }

        var response = _mapper.Map<IEnumerable<TreatmentResponse>>(data);

        return new SuccessApiResponse<IEnumerable<TreatmentResponse>>(response);
    }

In the above scenario, data count is 0 but not looped. enter image description here enter image description here

CodePudding user response:

When you want to check if your lists count is zero or not :

data.Count == 0

And as what PawZaw said, if you you an IEnumerable<> instead of a list :

data.Count() == 0

namespace you may need : using System.Linq;

  • Related