Entity Framework - Cannot convert lambda expression to type 'string' because it is not a delegate type
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.dllSamples (all of the开发者_Python百科se 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 typeAny ideas what would cause this?
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.
In my case it was missing
using System.Data.Entity;
using System.Linq;
using System.Data.Entity;
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.
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!
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 ()!!!
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
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.
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.
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;
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...
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.
Just Try using System.Linq; I think it will help you to sort out this issues.
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
精彩评论