Deal of the Day

Home » Main » Manning Forums » 2007 » LINQ in Action

Thread: Newbie about LINQ to SQL

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 0 - Pages: 1 Threads: [ Previous | Next ]
qiux

Posts: 1
Registered: 7/29/09
Newbie about LINQ to SQL
Posted: Jul 29, 2009 9:06 AM
  Click to reply to this thread Reply

Hi,

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.

Legend
Gold: 300 + pts
Silver: 100 - 299 pts
Bronze: 25 - 99 pts
Manning Author
Manning Staff
Manning Developmental Editor