Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Solve this Error "Cannot convert lambda expression to type 'string' because it is not a delegate type"

. 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

like image 465
peace Avatar asked Dec 02 '25 09:12

peace


1 Answers

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.

like image 61
Anthony Pegram Avatar answered Dec 05 '25 00:12

Anthony Pegram