Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Problem in Linq query with lamba expression

$
0
0

i try to fetch all employee from tblEmployee table
based on department name where depart_ref_id and emp_ref_id is stored on tblEmpOffInfo

1- tblEmployee.cs

public class tblEmployee
{
	public int ID { get; set; }
        public string FName { get; set; }
        public string LName { get; set; }

}

2- tblEmpOffInfo.cs

public class tblEmpOffInfo
{
public int Emp_ref_id { get; set; }
public string depart_ref_id{ get; set; }

}

3- tblDepartment.cs

public class tblDepartment
{
public int ID { get; set; }
public string DepartName{ get; set; }

}

and code

public ...
{
  var result = Context.tblEmployees.AsQueryable();
  if(search != null)
   {
     if (!string.IsNullOrEmpty(search.Department))
       {
          result = result.Where(x=> (x.ID == x.tblEmpOfficialInfoes.FirstOrDefault().Emp_ref_id) && (x.tblEmpOfficialInfoes.FirstOrDefault().Depart_ref_id == x.tblDepartments.FirstOrDefault().ID) && (x.tblDepartments.FirstOrDefault().DepartName.Contains(search.Department)));
       }
   }
}

but the query does not return the list of employees. i also tried this query but got stuck on select clause

result = from p in result
                             join o in Context.tblEmpOfficialInfoes on p.ID equals o.Emp_ref_id
                             join d in Context.tblDepartments on o.Depart_ref_id equals d.ID
                             where d.DepartName.Contains(search.Department)
                             select new {
                             result

                             };

Any have idea, Please Help


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>