我正在尝试将数据从数据库检索到我的网页。
我有 3 行:
**ser_num prod_num CustomerId UserName meter_type meter_count**
CAVC304222 M451nw 284 abc@m2.com Colour 5130
CAVC304222 M451nw 284 abc@m2.com Mono 11311
CNCX515111 HP 4200tn 284 abc@m2.com Mono 159527
在我的网页中它返回:
**ser_num prod_num CustomerId UserName meter_type meter_count**
CAVC304222 M451nw 284 abc@m2.com Colour 5130
CAVC304222 M451nw 284 abc@m2.com Colour 5130
CNCX515111 HP 4200tn 284 abc@m2.com Mono 159527
正如您所看到的,它返回的 CAV304222
两次与颜色完全相同。但它应该是 1 Color 1 Mono。
我假设我的 Controller 出了问题,但不确定:
Controller :
public ActionResult Index()
{
var model = cpctx.vw_ReadingsEntry.Where(w => w.UserName == this.User.Identity.Name).ToList();
return View(model);
}
}
有什么想法为什么它没有从数据库返回正确的数据,因为它忽略了仪表类型 Mono,而是引入了另一种颜色......
谢谢。
最佳答案
如果您只是在查找不同的行,那么您需要指定使行不同的原因。如果 ser_num 标识不同的记录,那么您可以执行以下操作。
public ActionResult Index()
{
var model = cpctx.vw_ReadingsEntry
.Where(w => w.UserName == this.User.Identity.Name)
.GroupBy(x => x.ser_num) //Replace this with your ser_num property
.Select(x => x.First())
.ToList();
return View(model);
}
}
希望这有帮助。
关于mysql - MVC 中结果不同时 SQL 返回相同结果两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26733527/