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

154,713

Solution 1

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

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

Solution 2

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

Solution 3

My case it solved i was using

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

using

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

will solve it

Solution 4

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

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.

Share:
154,713
Deep Sharma
Author by

Deep Sharma

I am a programmer i have no life. XoXoxo

Updated on January 20, 2020

Comments

  • Deep Sharma
    Deep Sharma over 4 years

    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

  • Ortund
    Ortund almost 10 years
    Thank you! That sorted me out :)
  • ThomasSquall
    ThomasSquall over 9 years
    It has worked for me :)
  • Laurence
    Laurence over 9 years
    got the same problem and your answer helped me fix it. Thanks.
  • Hamad
    Hamad over 9 years
    Why have you posted this as answer! when a different answer is accepted as correct answer?
  • Jack
    Jack over 9 years
    @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.
  • Ryan Kohn
    Ryan Kohn over 9 years
    @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.
  • Jack
    Jack over 9 years
    @RyanKohn: Thanks for reply. Here is the link of my question. Could you have a look at pls? stackoverflow.com/questions/28354301/…
  • BruceHill
    BruceHill almost 9 years
    @Hamad It is perfectly fine for people to post answers to questions that already have accepted answers. This answer does give another situation where the exception reported by the OP could occur and therefore could help someone else that is getting this exception.
  • elcool
    elcool over 8 years
    I was missing using System.Net; but this helped me to figure it out.
  • Ashok kumar
    Ashok kumar almost 8 years
    Excellent fix. Thank you for your information.
  • Liquid Core
    Liquid Core over 7 years
    This has to be the stupidest thing ever. Giving you a totally misleading error message instead of clearly stating that you miss a reference. Someone cannot program at microsoft. Well, thanks for the fix
  • Alan Waage
    Alan Waage over 7 years
    thanks, this fixed my circumstance
  • gvd
    gvd about 7 years
    Works for me, too. Visual Studio not detect this missing using.
  • Jawand Singh
    Jawand Singh about 7 years
    System.Data.Entity; this helped me, i was using Include() for eager loading. Thanks :)
  • Umpa
    Umpa over 6 years
    This fixed my problem! thanks.
  • Yusril Maulidan Raji
    Yusril Maulidan Raji over 6 years
    using System.Linq; fixed my issue. Thanks!
  • ShrapNull
    ShrapNull about 6 years
    Thank you, much appreciated, this really helped me
  • James Heffer
    James Heffer about 5 years
    calm down @Hamad...
  • ARLibertarian
    ARLibertarian about 5 years
    THANKS! Here's my iteration: dtSQLServers.AsEnumerable().Where(dr => dr["SourceSystem"].ToString() == "xxxxxxxxx").ToList()
  • Pharcyde
    Pharcyde over 3 years
    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.
  • chindirala sampath kumar
    chindirala sampath kumar about 3 years
    Took so much time to fix this simple issue. Definitely misleading error message. Adding using System.Linq fixed the issue.