Linq Query使用非实体对象获取数据

时间:2017-08-23 06:20:07

标签: asp.net-mvc entity-framework linq

我试图通过比较用户登录名来从3个表中获取记录

这是我的代码:

 public ActionResult MeritList() //departmental merit listed students details with status 1 
    {
        var username= HttpContext.Session["UserName"];
        List<StdListModel> model = new List<StdListModel>();
        var query = (from s in Context.tblStdDetails
                     join e in Context.tblStdEnrollments on s.ID equals e.StdReg_ref_id 
                     //join d in Context.tblDepartments on e.Depart_ref_id equals d.ID
                     where s.Status == '1' && e.tblDepartment.DepartName == username
                     select new StdListModel
                     {
                         ID = s.ID,
                         Name = s.Name,
                         FatherName = s.FatherName,
                         CNIC = s.CNIC,
                         FormNo = s.FormNo,
                         DiaryNo = s.DiaryNo,
                         Status = s.Status
                     }).ToList();
        foreach(var item in query)
        {
            model.Add(new StdListModel()
            {
                ID=item.ID,
                Name=item.Name,
                FatherName=item.FatherName,
                CNIC=item.CNIC,
                FormNo=item.FormNo,
                DiaryNo=item.DiaryNo

            });
        }
        return View(model);
    }

还尝试了此查询

var query = (from s in Context.tblStdDetails
                     join e in Context.tblStdEnrollments on s.ID equals e.StdReg_ref_id 
                     join d in Context.tblDepartments on e.Depart_ref_id equals d.ID
                     where s.Status == '1' && d.DepartName.Equals(username)
                     select new StdListModel
                     {
                         ID = s.ID,
                         Name = s.Name,
                         FatherName = s.FatherName,
                         CNIC = s.CNIC,
                         FormNo = s.FormNo,
                         DiaryNo = s.DiaryNo,
                         Status = s.Status
                     }).ToList();

但是它没有返回任何模型= 0,查询= 0,数据库具有正确的值,我也没有得到任何错误。

2 个答案:

答案 0 :(得分:0)

请用tolower()和trim函数检查用户名。

this.selectedBtn = true;

答案 1 :(得分:0)

我遇到了问题。它在

s.Status == '1'

我刚把它改成了

s.Status == 1 

它可以从数据库中获取数据。