我正在使用WEB API
数据库创建MySQL
。该数据库包括三个记录,一个仪表序列号,它的信号强度值和日期时间。我想发送仪表序列号,然后获取该序列号的所有记录。我成功发送序列号将URL,但我只获得多次单个记录。以下是控制器代码。
public MDCEntities medEntitites = new MDCEntities();
public HttpResponseMessage GetByMsn(string msn)
{
try
{
return Request.CreateResponse(HttpStatusCode.Found, medEntitites.tj_xhqd.Where(m=> m.zdjh == msn));
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
}
我得到的回应是
{
"zdjh": "000029000033",
"sjsj": "2017-09-20T00:01:54",
"xhqd": "28"
},
{
"zdjh": "000029000033",
"sjsj": "2017-09-20T00:01:54",
"xhqd": "28"
},
{
"zdjh": "000029000033",
"sjsj": "2017-09-20T00:01:54",
"xhqd": "28"
},
{
"zdjh": "000029000033",
"sjsj": "2017-09-20T00:01:54",
"xhqd": "28"
},
.
.
.
.
.
// same values are repeating again and again.
我试图看到解决方案,但我无法从中获得帮助。
任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
一次又一次搜索后,我设法通过更改查询来解决问题
TO:
return Request.CreateResponse(HttpStatusCode.Found, medEntitites.tj_xhqd.Where(m=> m.zdjh == msn));
这
return Request.CreateResponse(HttpStatusCode.Found, medEntitites.tj_xhqd.Where(m=> m.zdjh == msn).Where(o=> o.sjsj <= dt).Select(m=> new { m.zdjh , m.sjsj, m.xhqd }).Distinct());