views:

109

answers:

2

. I get this error: "Cannot convert lambda expression to type 'string' because it is not a delegate type" - keyword select become underlined in blue Can you please advice.

   Employee emp = new Employee();
   comHandledBySQt.DataSource = from x in emp.GetDataFromTable("1")
                    select new { x.Id, Name = x.FirstName + " " + x.LastName };
   comHandledBySQt.DisplayMember = "Name";
   comHandledBySQt.ValueMember = "Id";

Above code should displays drop list of employees first name and last name in a combo box

+2  A: 

x.Id might need to be made an assignment, eg.
select new { Id = x.Id, Name = string.Format("{0} {1}", x.FirstName, x.LastName) };

DataSet is not enumerable, so if GetDataFromTable("1") returns DataSet, you need to enumerate over the appropriate result set / table, eg. GetDataFromTable("1").Tables[0] if you only have one result set.

Each element in the enumeration will then be a DataRow, which has an index (rather than property) accessor: DataRow[columnIndex] or DataRow[columnName].

Phong
Didn't work. Just to let you know GetDataFromTable returns DataSet. I will see what i can do. Thanks anyway.
peace
And it's not accurate. Merely stating `x.Id` will automatically create an `Id` property within the anonymous type in this instance. The issue is elsewhere.
Anthony Pegram
A: 

If you have a strongly typed dataset, you can absolutely perform a query on the named members of a table in a manner close to what you have tried.

var queryA = (from x in dataSet.EmployeeTable
                        select new { x.Id, Name = x.FirstName + " " + x.LastName }).ToList();

From your given error, it does not appear that you have a strongly-type dataset, and it could very well be that your return value is not a DataSet but rather just a DataTable (at least, from my attempts at recreating your error message, I was getting it on the DataTable and not the set). But without a strongly-typed DataSet/DataTable, this is the query you would perform.

var queryB = (from DataRow x in someSet.Tables[0].Rows
            select new { Id = (string)x["Id"], Name = (string)x["FirstName"] + " " + (string)x["LastName"] }).ToList();

Notice that in both instances you would include a .ToList() call, as without it you get another error saying that the complex databinding requires an IList or IListSource, which the query (without the ToList() call) is neither.

Anthony Pegram
dont forget to check for DBNull!
Muad'Dib