Moeez
Moeez

Reputation: 478

Finding null records for Json return string

I have created a WEB API using MySQL DB. On the basis of data I am sending YES or NO.

How API is working?

I am passing a meter serial number and a date time. The API receives both of them. +- The minutes in time and display all the records withing that time.

What's the issue ?

I am always getting YES response regarding whatever I sent the API.

Below is my code

 public class ResponseChecker
{
    public string Response { get; set; }

    public ResponseChecker(IEnumerable result)
    {

        if (result.ToString().ToList().Count()>0)
        {
            Response = "Yes";
        }
        else
        {
            Response = "No";
        }
    }
}

public MDCEntities medEntitites = new MDCEntities();
 try
        {
            var before = dt.AddMinutes(-5);
            var after = dt.AddMinutes(5);

            var result = medEntitites.tj_xhqd
                         .Where(m =>
                         m.zdjh == msn &&
                         m.sjsj >= before &&
                         m.sjsj <= after).Select(m => new { MSN = m.zdjh, DateTime = m.sjsj, Signal_Strength = m.xhqd }).Distinct();


            return Request.CreateResponse(HttpStatusCode.OK, new { data = new ResponseChecker(result)});
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
        }

The response I am getting is like below

{
"data": {
    "Response": "YES"    }
}

I have tried each and everything for checking the any null record in the result as I tired == null , != null, .Any() etc. But still unable to get my desired result.

Also the part result.ToString().ToList().Count()>0 is always giving me true

Any help would be highly appreciated.

Upvotes: 2

Views: 32

Answers (1)

abr
abr

Reputation: 2129

Have you tried printing what's the result of this?

if (result.ToString().ToList().Count()>0)

You can do:

var result = medEntitites.tj_xhqd
                     .Where(m =>
                     m.zdjh == msn &&
                     m.sjsj >= before &&
                     m.sjsj <= after).Select(m => new { MSN = m.zdjh, DateTime = m.sjsj, Signal_Strength = m.xhqd }).Distinct().ToList();

And just validate the ammount of result with a simple count.

Upvotes: 1

Related Questions