75
votes

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?

15
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
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
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. <sigh/>Keith Barrows

15 Answers

115
votes

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.

105
votes

In my case it was missing

using System.Data.Entity;

13
votes
using System.Linq;
using System.Data.Entity;
7
votes

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.

7
votes

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!

2
votes

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 ()!!!

1
votes

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

1
votes

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

0
votes

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.

0
votes

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.

0
votes

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;
0
votes

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

0
votes

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.

0
votes

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

0
votes

For .Net Core just introduce;

using System.Linq;
using Microsoft.EntityFrameworkCore;