【问题标题】:Model data not returning null when empty模型数据为空时不返回 null
【发布时间】:2016-01-29 21:50:35
【问题描述】:

所以我正在查询我的数据库,对于返回,我有两种不同的可能性,一种用于模型集是否为空,另一种用于是否有信息。但是,当我使用空表运行它时,我仍然得到认为它有数据的响应。

查询可以正常工作,但每次调用成功 = [] 或调用有数据成功时,我都会得到相同的响应。

IEnumerable<Details> detail = db.Details Where(blah blah blah).ToList().Select(a => new Details { blah blah blah });

var ResponseTrue = new DetailResponse() { status = true, statusMessage = "Success", calls = detail };
var ResponseFalse = new DetailResponse() { status = false, statusMessage = "Set is Empty", calls = detail };

if (detail != null)
{
    return Ok(ResponseTrue);
}

return Ok(ResponseFalse);

【问题讨论】:

  • 什么是 responseFalse?
  • 哦,我的错,让我编辑一下,第二个应该是假的。

标签: asp.net-web-api2


【解决方案1】:

尝试使用 Any()

if (!detail.Any())
{
    //your code goes here
}

【讨论】:

  • 第一次它不起作用,但纯粹是用户错误。我必须阅读更多内容到.Any() 谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
相关资源
最近更新 更多