programing

Entity Framework-람다식이 대리자 형식이 아니기 때문에 '문자열'형식으로 변환 할 수 없습니다.

nasanasas 2020. 10. 25. 12:24
반응형

Entity Framework-람다식이 대리자 형식이 아니기 때문에 '문자열'형식으로 변환 할 수 없습니다.


내 C # 기반 코드에서 Entity Framework를 사용하고 있습니다. 예상치 못한 이상 함이 발생하여 제안을 찾고 있습니다.

사례 1, 2, 3, 4 ... 프로젝트 :
RivWorks.dll
RivWorks.Service.dll
RivWorks.Alpha.dll

샘플 (이 모든 작업) :
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();
}

기타

사례 "이상 함":
RivWorks.Web.Service.dll (WCF 프로젝트)
다른 프로젝트와 동일한 참조를 포함합니다.

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();
    ...
}

이 컴파일 시간 오류가 발생합니다 (편집기에서 "where"라는 단어가 강조 표시됨) : 대리자 형식이 아니기 때문에 람다 식을 '문자열'형식으로 변환 할 수 없습니다 .

이것이 원인이 될 아이디어가 있습니까?


결과에 관심이있는 사람들을 위해 :
코드 헤드에 간단한 Using 문이 없습니다.

using System.Linq;

이것은 그것을 바로 고쳤습니다.


제 경우에는 누락되었습니다

using System.Data.Entity;


using System.Linq;
using System.Data.Entity;

제 경우에는

Using System.Linq;

하지만 where 절 항목 뒤에 &&가 누락되었습니다.

잘못된 코드 :

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

올바른 코드 (오류 없음) :

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

나는 이것이 누군가 시간을 절약하기를 바랍니다.


나는 약 한 시간 동안 Razor 뷰 내의 Telerik Grid 템플릿에서이 문제를 해결하기 위해 고심하고있었습니다. 제 경우에는 다음과 같습니다.

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

이것은 다음과 같아야했습니다.

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

"이드"사건이 잘못 됐어! 누군가에게 도움이되기를 바랍니다. 존재하지 않는 속성을 입력했기 때문에이 오류가 발생할 수 있습니다!


나는 이것을 우연히 발견하고 다른 해결책을 찾았습니다. 나는 var query = context.Contacts.Where(c => c.FullNameReverse == "TingTong");언급 된 오류를 사용 하고 받고 있었습니다 . 실수는 내가 방법 FullNameReverse()을 속성 으로 사용하고 있었다는 것 FullNameReverse입니다. ()를 놓쳤다 !!!


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.

참고URL : https://stackoverflow.com/questions/2058487/entity-framework-cannot-convert-lambda-expression-to-type-string-because-it

반응형