Cannot convert lambda expression to type 'string' because it is not a delegate type

C#asp.netLinqLambda

C# Problem Overview


I am using a LINQ lambda expression like so:

int Value = 1;
qryContent objContentLine;

using (Entities db = new Entities())
{
    objContentLine = (from q in db.qryContents
                      where q.LineID == Value
                      orderby q.RowID descending
                      select q).FirstOrDefault();
}
    

However, I am getting the following error:

> Cannot convert lambda expression to type 'string' because it is not a delegate type

C# Solutions


Solution 1 - C#

I think you are missing using System.Linq; from this system class.

and also add using System.Data.Entity; to the code

Solution 2 - C#

In my case, I had to add using System.Data.Entity;

Solution 3 - C#

My case it solved i was using

@Html.DropDownList(model => model.TypeId ...)  

using

@Html.DropDownListFor(model => model.TypeId ...) 

will solve it

Solution 4 - C#

If it's not related to missing using directives stated by other users, this will also happen if there is another problem with your query.

Take a look on VS compiler error list : For example, if the "Value" variable in your query doesn't exist, you will have the "lambda to string" error, and a few errors after another one more related to the unknown/erroneous field.

In your case it could be :

objContentLine = (from q in db.qryContents
                  where q.LineID == Value
                  orderby q.RowID descending
                  select q).FirstOrDefault();

Errors:

> Error 241 Cannot convert lambda expression to type 'string' because it is not a delegate type > > Error 242 Delegate 'System.Func<..>' does not take 1 arguments > > Error 243 The name 'Value' does not exist in the current context

Fix the "Value" variable error and the other errors will also disappear.

Solution 5 - C#

For people just stumbling upon this now, I resolved an error of this type that was thrown with all the references and using statements placed properly. There's evidently some confusion with substituting in a function that returns DataTable instead of calling it on a declared DataTable. For example:

This worked for me:

DataTable dt = SomeObject.ReturnsDataTable();

List<string> ls = dt.AsEnumerable().Select(dr => dr["name"].ToString()).ToList<string>();

But this didn't:

List<string> ls = SomeObject.ReturnsDataTable().AsEnumerable().Select(dr => dr["name"].ToString()).ToList<string>();

I'm still not 100% sure why, but if anyone is frustrated by an error of this type, give this a try.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeep SharmaView Question on Stackoverflow
Solution 1 - C#S. S. RawatView Answer on Stackoverflow
Solution 2 - C#skbView Answer on Stackoverflow
Solution 3 - C#user4238584View Answer on Stackoverflow
Solution 4 - C#AFractView Answer on Stackoverflow
Solution 5 - C#MatthewView Answer on Stackoverflow