132

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

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
Deep Sharma
  • 3,374
  • 3
  • 29
  • 49

5 Answers5

362

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

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

Mr Nobody
  • 359
  • 3
  • 9
  • 23
S. S. Rawat
  • 5,943
  • 4
  • 43
  • 59
  • I actually had this issue just a few minutes prior and I had usings for System.Linq and System.Data.Entity. I had to remove them, run a failed build and then readd them to the file. Then it worked. – Pharcyde Jan 13 '21 at 15:21
99

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

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
skb
  • 1,148
  • 2
  • 9
  • 17
  • 2
    @Ryan Kohn I have applied all the solution methods described here, but none of them not solved my problem. So, could you have a look at please described on Kendo UI : Cannot convert lambda expression to type 'string' because it is not a delegate type? Thanks in advance. – Jack Feb 05 '15 at 14:31
  • @H.Johnson Note that I only edited this answer. *skb* is the original author. On another note, if your problem is not identical to the one described in the question, you can consider asking a new question to the community. – Ryan Kohn Feb 05 '15 at 19:28
  • @RyanKohn: Thanks for reply. Here is the link of my question. Could you have a look at pls? http://stackoverflow.com/questions/28354301/convert-lambda-expression-to-json-in-mvc – Jack Feb 05 '15 at 21:25
  • I was missing `using System.Net; ` but this helped me to figure it out. – elcool Aug 14 '15 at 17:12
21

My case it solved i was using

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

using

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

will solve it

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
user4238584
  • 221
  • 2
  • 2
9

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.

Community
  • 1
  • 1
AFract
  • 8,868
  • 6
  • 48
  • 70
5

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.

Matthew
  • 194
  • 1
  • 3
  • THANKS! Here's my iteration: dtSQLServers.AsEnumerable().Where(dr => dr["SourceSystem"].ToString() == "xxxxxxxxx").ToList() – ARLibertarian Apr 22 '19 at 18:01