问题描述:

I wish to have an If statement in my code, which tests if any results are obtained from my SQL query. I have tried looking on Google but have not found any effective way to test this.

My code at the moment always goes into the If block and tried to display the page. What have I done wrong ?

var pendingMeetings = db.Query("SELECT * FROM table");

if (pendingMeetings != null) {

//display page

} else {

<h1>No results found!</h1>

}

网友答案:

try

if (pendingMeetings.Any())
{
     // display page
}
else 
{
    <h1>No results found!</h1>
}
网友答案:

You get a resultset with no rows (but will get the column names).

See Bala R's answer for a solution.

网友答案:

Try changing your if statement to

if (pendingMeetings != null && pendingMeetings.Count() > 0)
{ 
    //snip
}

db.Query returns an IEnumerable<object>

相关阅读:
Top