The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

qiux (1) [Avatar] Offline

I have several questions about LINQ to SQL.

I have an Employee Class which has some methods as below.

Public Class Employee {

public static System.Data.Linq.Table<Employee> GetEmployeeTable()
NorthWindDataClassesDataContext dc = new NorthWindDataClassesDataContext();
return dc.GetTable<Employee>();

public static System.Data.Linq.Table<Employee> GetEmployeeBy(int intID)
NorthWindDataClassesDataContext dc = new NorthWindDataClassesDataContext();
var query = from n in dc.Employees
where n.EmployeeID == intID
select n;
return query; //error at here....
//Cannot implicitly convert type 'System.Linq.IQueryable<L2S_Northwind.Employee>' to
//'System.Data.Linq.Table<L2S_Northwind.Employee>'. An explicit conversion exists (are you missing a cast?)


public static System.Data.DataTable FilterData(Table<Employee> tbl, string strName)
var query = from n in tbl
where n.FirstName == strName
select n;
return query; //error at here
//Cannot implicitly convert type 'System.Linq.IQueryable<L2S_Northwind.Employee>' to 'System.Data.DataTable'.
//An explicit conversion exists (are you missing a cast?)


public static System.Data.DataTable FilterData(System.Data.DataTable dt, string strName)
//how to query at here?
//if I want to select if the FirstName == strName


And I have an Employee Form which has a datagridview for showing data from database.
1. The first part works awesome, I'm happy cause this is the first time I tried LINQ To sql due to before I use SQLDataAdapter and SQLCommand objects.
System.Data.Linq.Table<Employee> employee = Employee.GetEmployeeTable();
dataGridView1.DataSource = employee; //okay.. this works.. all data has been showed at here..

Now the I can't continue my project due to I can't do LINQ to SQL with below problems (Filtering feature).
2. //Now the question is, in this form, I provided a filtering feature so user can filter data from datagridview.
//so I intend to do that with this example, I want to filter the FirstName of Employees, who else called DAVID

//My thinking is like this 1st option
System.Data.DataTable dt = Employee.FilterData(employee, "DAVID"); //this use Table<Employee>
dataGridView1.DataSource = dt;

//And this is my 2nd option thinking
//....... I don't know the script
//but all I want to do is.. I intend to convert datagridview back to datatable so I can use LINQ to datatable.
//which I can call this method 'Employee.FilterData(thisDataTable, "DAVID") //this use DataTable
//the 1st and 2nd options all I want to do is query in local datatable instead to sql server, because it will cause round trip and will affect performance

3. //well, the last question is how can I query like this method, I want to query all the data which the employee id is 1
dataGridView1.DataSource = Employee.GetEmployeeBy(1);

Please help to assist me about LINQ to SQL and DataTable, hope I explained well.

Thank you.