76

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?

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
Keith Barrows
  • 24,802
  • 26
  • 88
  • 134
  • That does sound strange. If you remove the call to `FirstOrDefault`, what happens? Obviously it'll fail where you try to use `product` afterwards, but does that statement compile? – Jon Skeet Jan 13 '10 at 16:52
  • Also, if you change it to `var product = _dbFeed.AutoWithImage.Where(a => a.AutoID == result);` what happens then? Let's take query expressions out of the mix... – Jon Skeet Jan 13 '10 at 16:53
  • 1
    All of those examples fail. However, I went through the Using statements in all of my code pieces and discovered I was missing one: Using System.Linq; That fixed the error. – Keith Barrows Jan 13 '10 at 17:48

17 Answers17

116

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.

Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93
Keith Barrows
  • 24,802
  • 26
  • 88
  • 134
  • 18
    I'm surprised that neither VS nor ReSharper are smart enough to detect this condition. Thankfully SO is smarter than both. – bwerks May 08 '11 at 06:16
  • 2
    I'm getting the same err msg, and I have (and "always did have") using System.LINQ. Of course, one of the model members changed data types from string[] to string... – B. Clay Shannon-B. Crow Raven Aug 07 '13 at 21:21
106

In my case it was missing

using System.Data.Entity;

Miguel Galante
  • 1,689
  • 1
  • 14
  • 13
13
using System.Linq;
using System.Data.Entity;
Dorathoto
  • 201
  • 7
  • 17
7

In my case, I had the

Using System.Linq;

but I was missing the && after a where clause item.

Bad Code:

item.Group.ID == grp.ID
p.Product_Status_Flag == 1 &&
item.Valid

Correct Code ( with no error ):

item.Group.ID == grp.ID && // <- This was missing and I didn't see it right away.
p.Product_Status_Flag == 1 &&
item.Valid

I hope this saves someone some time.

jhamm
  • 1,858
  • 21
  • 19
7

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!

Christopher
  • 10,409
  • 13
  • 73
  • 97
2

I stumbled upon this and found a different fix. I was using var query = context.Contacts.Where(c => c.FullNameReverse == "TingTong"); and getting the mentioned error. The mistake was I was using the method FullNameReverse() as property FullNameReverse. Missed the ()!!!

Austin Henley
  • 4,625
  • 13
  • 45
  • 80
FastDev
  • 21
  • 1
1

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...

Ragnorok
  • 57
  • 6
1

i had the same problem with mvc 3 razor maybe someone has the same so i wanna show how to fix it in my stuation

List<Taksit> lst = db.Taksit.Where(y => y.OgrenciId.Equals(Convert.ToInt32( list[0].Id))).ToList();

i tried to use contains but hence OgrenciId is int i get the error mentioned here so by using equals the problem is solved

mathel
  • 29
  • 2
0

In my case I had this error when trying to use Include in clientContext.Load in a Sharepoint 2013 app.

I had included Sharepoint client library like so:

using SP = Microsoft.SharePoint.Client;

To fix, in addition I also added it without the namespacing:

using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
Demelziraptor
  • 1,545
  • 1
  • 14
  • 20
0

My issue involved the format:

.Columns(columns => {
    columns.Bound(p => p.Id).Filterable(false);
    columns.Bound(p => p.Name).Width(250);
    ...

Every p.Whatever had this error on it.

This was a project using MVC, and I found my issue was having "public static int" or "public static string" on these variables (Id, Name, etc.) in my model. When I removed "static" on all my model variables, I no longer got the error. Was driving me nuts for about a day...

vapcguy
  • 7,097
  • 1
  • 56
  • 52
0

I had this problem in a slightly different version.
Should you call a (static) method from inside your lambda, check its return type. If the return type should be an IEnumerable (which often is true when using lambdas) but you return object, obviously you have a problem.

Andi Truman
  • 111
  • 1
  • 6
0

Just Try using System.Linq; I think it will help you to sort out this issues.

Jinto John
  • 365
  • 4
  • 22
0

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.

Craig Fisher
  • 1,681
  • 2
  • 19
  • 26
0

For .Net Core just introduce;

using System.Linq;
using Microsoft.EntityFrameworkCore;
lucky
  • 12,734
  • 4
  • 24
  • 46
0

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.

Gabe
  • 837
  • 9
  • 13
0

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

0

I had a similar looking problem but with Rx and in my case adding

using System;

helped. FWIW

Mess with those extension methods missing is too annoying sometimes.

Valentin Kuzub
  • 11,703
  • 7
  • 56
  • 93