我正在尝试使用元组,因此我可以在同一个视图中使用2个模型,我在控制器中执行了此操作:
public ActionResult Index( int id = 0 )
{
int lastVoyageID = entities.log_voyage.Max(item => item.Num_Voy);
var first = entities.log_voyage.Where(v => v.Num_Voy == lastVoyageID);
log_voyage voyage1 = entities.log_voyage.Find(lastVoyageID);
var second = entities.log_ligne_voyage.Where(x => x.NUM_Voy == lastVoyageID).ToList();
var t = new Tuple<log_voyage, log_ligne_voyage>(first, second);
return View(t);
}
但我得到了这个:
我不知道该怎么办?我如何使用元组?
这是错误:
无法将system.Linq.IQueryable转换为WebApplication.Models.log_voyage
答案 0 :(得分:0)
var first = entities.log_voyage.Where(v => v.Num_Voy == lastVoyageID)
返回集合,但是Tuple期望单个对象,所以它应该是;
var first = entities.log_voyage.Where(v => v.Num_Voy == lastVoyageID).FirstOrDefault()
此表达式返回集合,而Tuple再次寻找一个对象
var second = entities.log_ligne_voyage.Where(x => x.NUM_Voy == lastVoyageID).ToList()
您可能希望将元组签名更改为Tuple<log_voyage, List<log_ligne_voyage>>
最后,
public ActionResult Index( int id = 0 )
{
int lastVoyageID = entities.log_voyage.Max(item => item.Num_Voy);
var first = entities.log_voyage.Where(v => v.Num_Voy == lastVoyageID).FirstOrDefault();
log_voyage voyage1 = entities.log_voyage.Find(lastVoyageID);
var second = entities.log_ligne_voyage.Where(x => x.NUM_Voy == lastVoyageID).ToList();
var t = new Tuple<log_voyage, List<log_ligne_voyage>>(first, second);
return View(t);
}