Getting Custom Column from IQueryable DB First Approach EF

288 views Asked by At

I am working on Database First Approach in Entity Framework where I have to retrieve specific columns from the Entity.

Public IQueryable<Entity.Employees> GetEmployeeName(String FName,String LName)
{
     var query = (from s in Employees
                  where s.firstName = FName && s.lastName = LName
                  select new {s.firstName, s.middleName});
     return query;
}

Here return statement is throwing an error where it seems that its not matching with Employees (entity) columns. Could you please help me in sorting out this issue? Thanks in advance.

1

There are 1 answers

3
Mazhar Qayyum On

You need to use == for comparison, also you need to use dynamic type as return type since you are returning a custom anonymous type. Try this

Public IQueryable<dynamic> GetEmployeeName(String FName,String LName)
{
var query=(from s in Employees
where s.firstName==FName && s.lastName==LName
select new {s.firstName,s.middleName});
return query.AsQueryable();
}

Finally you will use it like below, keep in mind that intelisense won't work on dynamic object.

var query = GetEmployeeName("Jake", "Smith");
            List<dynamic> results = query.ToList();
            foreach (dynamic result in results)
            {
                string fristName = result.FirstName;
                string lastName = result.MiddleName;
            }