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

Viewed 89564

I am using Entity Framework in my C# based code. I am running into an unexpected weirdness and am looking for suggestions.

Case 1, 2, 3, 4... Projects:
RivWorks.dll
RivWorks.Service.dll
RivWorks.Alpha.dll

Samples (all of these work):
RivWorks.Alpha.dll:

public static bool EndNegotitation(long ProductID)
{
    var product = (from a in _dbFeed.AutoWithImage 
                   where a.AutoID == ProductID select a).FirstOrDefault();
...
}

RivWorks.Service.dll

public static RivWorks.Model.NegotiationAutos.AutoWithImage 
    GetProductById(long productId)
{
    var myProduct = from a in _dbFeed.AutoWithImage 
                    where a.AutoID == productId select a;

    return myProduct.FirstOrDefault();
}
public static List<RivWorks.Model.NegotiationAutos.AutoWithImage> 
    GetProductByCompany(Guid companyId)
{
    var myProduct = from a in _dbFeed.AutoWithImage 
                    where a.CompanyID == companyId select a;

    return myProduct.ToList();
}

etc

Case "weirdness":
RivWorks.Web.Service.dll (WCF project)
Contains the same references as the other projects.

public NegotiateSetup GetSetup(string method, string jsonInput)
{
    ...
    long.TryParse(ProductID, out result);
    var product = (from a in _dbFeed.AutoWithImage 
                   where a.AutoID == result select a).FirstOrDefault();
    ...
}

I am getting this compile time error (the word "where" is highlighted in my editor):
Cannot convert lambda expression to type 'string' because it is not a delegate type

Any ideas what would cause this?

17 Answers

For those interested in the outcome:
I was missing a simple Using statement at the head of my code.

using System.Linq;

This fixed it right up.

I was struggling with this in a Telerik Grid template within a Razor view for about an hour. In my case, this:

columns.Bound(x => x.ID).Template(@<text><a href="@(Model.AppUrl + AdditionalFeeTypes/Details/" + item.ID)">@item.ID</a></text>);

was supposed to be this:

columns.Bound(x => x.Id).Template(@<text><a href="@(Model.AppUrl + AdditionalFeeTypes/Details/" + item.Id)">@item.Id</a></text>);

The case on "Id" was wrong! I hope this helps someone. You might be getting this error just because you put a non-existent property!

Thread's a bit old, but I only just encountered this, and nothing on the 'net was the answer. One site mentioned what lead to the answer, which was a data type issue, but sadly I can't find it again, so I'm posting my solution here. Maybe some future searcher will derive benefit from it.

Original: IQueryable test = from r in Records where r.Record_ID == 100 select r;

where Records is an IQueryable resulting from a prior LINQ expresson.

The fix is to cast Records: (IQueryable<record>)Records in the expression. Having found it, it makes perfect sense. Records isn't typed so the LINQ has no clue if r.Record_ID is valid. The confusion is the error message, which appears all over the 'net in dozens of places, in nearly every case the solution being one of the two using clauses missing. The one or two I found that were not an using issue, they didn't bother to post what fixed it.

Hope this helps...

I was having a similar problem binding columns to a Telerik MVC Grid. I had an Id property on my ViewModel class. (Inspired by Chris's answer above) I renamed it to XxxId and the problem went away. I seem to recall something about MVC doing something special for Id properties.

For .Net Core just introduce;

using System.Linq;
using Microsoft.EntityFrameworkCore;

I got this in a pretty unique situation but maybe it'll help someone. I'm using Entity Framework within a .NET Standard 2.0 application. In order to reverse engineer (database first) and scaffold the entities you have to use a separate dummy project. In the dummy project I was using Microsoft.EntityFrameworkCore[Tools/SqlServer/Design] version 5.09 Nuget packages in order for the Scaffold-DbContext command to work. The problem was that it would generate a DBContext file with code that caused this error to be thrown. This version of EntityFrameworkCore is not compatible with .NET Standard 2.0 and the solution was use the 3.1.18 versions which are compatible.

If you got this issue from the auto-generated database context class that was generated after reverse engineer EF, then you have to check your Microsoft.EntityFrameworkCore.SqlServer version. some versions are not supported entity.HasIndex(). Update your Nuget package of

  • Microsoft.EntityFrameworkCore.SqlServer to 5.0.8

  • Microsoft.EntityFrameworkCore.Tools to 5.0.8

Related