tags:

views:

63

answers:

3

I am new to LINQ and am trying to query my Business Layer (BLL), i.e. Model. I have looked at some samples, but none seem to assist me in my scenario.

Let's say I start w/ creating a method, I would think:

public void GetLinqQuery()
{
EmployeeCollection employeeList = EmployeeController.GetAll();  // my usual way of getting employee list

// now I'll attempt using LINQ to get a subset of that collection
var query = from emp in employeeList
    select emp.FirstName,      // *** Intellisense breaks here, why?
}

Am I doing something wrong? How can I query my Collections/Entities given that I have Objects?

Thanks!

A: 

It would probably be easier to use an IEnumerable<Employee> or IQueryable<Employee>. Try the following:

public void GetLinqQuery()
{
IEnumerable<Employee> employeeList = EmployeeController.GetAll() as IEnumerable<Employee>;

var query = from Employee emp in employeeList
            select emp.FirstName      // Does intellisense break now?
}
Tomas Lycken
Why would that be easier? FYI, my collections implement: IBindingListView which in turn ultimately inheret from IEnumerable.
OK - well, I wasn't sure that your collection implemented IEnumerable, but if it does it won't make a difference. Is it when you type the comma that you don't get any intellisense, or before that?
Tomas Lycken
A: 

When you write the following code

Employee[] Employees= GetEmployee();
var query =
from c in Employees
select c.Name;

the compiler generates this code:

Employee[] Employees= GetEmployee();
IEnumerable<Employee> query =
Employees.Select( c => c.Name);

So you can use any one of the above..

Even you can use typed List collection

List<Employee> Employees = query.ToList();
miti737
A: 

If I'm interpretting this correctly, it looks like you are trying to do:

var query = from emp in employeeList
  select emp.FirstName, emp.LastName;

That won't work. You need to select it as a new object, either anonymous or some named type:

var query = from emp in employeeList
  select new { emp.FirstName, emp.LastName };
Console.WriteLine(query.First().FirstName);

If your intention is to return the query to the calling function you'll need to use a named type, as anonymous types can't leave the scope they are in:

return from emp in employeeList
  select new EmployeeData(emp.FirstName, emp.LastName);
Talljoe
Works perfectly - thanks!