linq加入ASP MVC [英] linq join in ASP MVC

查看:109
本文介绍了linq加入ASP MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mvc的新手:)

我尝试显示加入但未成功.

I try to show a join but was not successfull .

我将下面的方法放在模型中

I put below method in Model

LinqDataContext db = new LinqDataContext();
    public IQueryable getExam()
    {
        return from exam in db.Exam_Table
               join cat in db.Cat_Table
               on
               exam.Exam_Cat_ID_FK equals cat.Cat_ID

               select new { exam, cat };        

    }

并通过以下代码从控制器调用它:

and call it from controller by below code :

   exam_rep exrep = new exam_rep();
    public ActionResult Index()
    {
        var exams=exrep.getExams();
        return View(exams);
    }

,但无法通过以下代码在视图中显示它:

but can not display it in view by below code :

foreach(var ex in Model)
{
%>
  <tr>
  <td><%: ex.exam.Exam_Title %></td>
  </tr>

<%   
}
%>

我应该如何在视图中显示值?

how should I display values in view?

推荐答案

不是由linq数据上下文链接的对象吗?

Aren't the objects linked by the linq datacontext?

我认为应该是这样的:

db.Exams.Select(e => new { Exam = e, Cat = e.Cat });

Linq语法

from e in db.Exams select new { Exam = e, Cat = e.Cat };

或者简单地

// assuming Exam.Cat
return db.Exams

我会查看您的数据上下文,并确保正确设置了它.看来您可能正在使事情复杂化.

I would look at your datacontext and make sure you have it setup correctly. It seems like you might be over complicating things.

这篇关于linq加入ASP MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆