2

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

result.ToString().ToList().Count()>0 it's always sending me true
write return result.ToString().ToList().Count() and print it. then write result.ToString(); and print it. You're interpreting a var as a string, then as a list and counting - do you see what's wrong with this sentence?
Yes I have corrected it now. You are a life savor :)
Glad It helped!

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.