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

后端 未结 15 2213
再見小時候
再見小時候 2020-11-30 09:18

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

相关标签:
15条回答
  • 2020-11-30 09:55

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

    0 讨论(0)
  • 2020-11-30 09:55

    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 讨论(0)
  • 2020-11-30 09:57

    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 讨论(0)
  • 2020-11-30 10:00

    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.

    0 讨论(0)
  • 2020-11-30 10:02

    In my case it was missing

    using System.Data.Entity;

    0 讨论(0)
  • 2020-11-30 10:03

    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 讨论(0)
提交回复
热议问题