. 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.FirstNa开发者_Python百科me + " " + 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
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.
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]
.
精彩评论