locked
Linq Query to Fetch data from non entity object RRS feed

  • Question

  • User210895818 posted

    I'm trying to fetch the record from 3 tables by comparing the user Logged in name

    Here is my code

    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 
                         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);
        }

    Also Tried this Query

    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();

    But it does not return anything model=0, query =0, the database has right values and I don't get any error either.

    Wednesday, August 23, 2017 8:03 AM

Answers

  • User210895818 posted

    I just changed

    s.Status == "1"

    into 

    s. Status == 1

    and it work

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 23, 2017 10:54 AM

All replies

  • User-1716951449 posted

    if u change this

    where s.Status == '1' && e.tblDepartment.DepartName == username

    to (remove the department filter)

    where s.Status == '1'

    u get result?

    Wednesday, August 23, 2017 9:20 AM
  • User210895818 posted

    Hi Kelmen,

    Thanks For Your Reply

    u get result?

    No, I don't get the result

    Wednesday, August 23, 2017 9:32 AM
  • User210895818 posted

    I just changed

    s.Status == "1"

    into 

    s. Status == 1

    and it work

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 23, 2017 10:54 AM