Programming

LINQ to Entities가 방법을 인식하지 못합니다

procodes 2020. 8. 4. 19:34
반응형

LINQ to Entities가 방법을 인식하지 못합니다


linq 쿼리를 수행하려고 할 때 다음 오류가 발생합니다.

LINQ to Entities는 'Boolean IsCharityMatching (System.String, System.String)'메소드를 인식하지 못하므로이 메소드를 상점 표현식으로 변환 할 수 없습니다.

사람들이 동일한 오류가 발생하는 이전의 많은 질문을 읽었으며이를 올바르게 이해하면 LINQ to Entities가 전체 linq 쿼리 표현식을 서버 쿼리로 변환해야하므로 외부 메소드를 호출 할 수 없기 때문입니다. 그 안에. 나는 내 시나리오를 아직 효과가있는 것으로 바꿀 수 없었고 뇌가 녹기 시작했기 때문에 누군가 나를 올바른 방향으로 가리킬 수 있기를 바랐습니다. 우리는 Entity Framework와 사양 패턴을 사용하고 있습니다.

사양을 사용하는 코드는 다음과 같습니다.

ISpecification<Charity> specification = new CharitySearchSpecification(charityTitle, charityReference);

charities = charitiesRepository.Find(specification).OrderBy(p => p.RegisteredName).ToList();

linq 표현식은 다음과 같습니다.

public System.Linq.Expressions.Expression<Func<Charity, bool>> IsSatisfied()
{
    return p => p.IsCharityMatching(this.charityName, this.charityReference);
}

IsCharityMatching 방법은 다음과 같습니다.

public bool IsCharityMatching(string name, string referenceNumber)
{
    bool exists = true;

    if (!String.IsNullOrEmpty(name))
    {
        if (!this.registeredName.ToLower().Contains(name.ToLower()) &&
            !this.alias.ToLower().Contains(name.ToLower()) &&
           !this.charityId.ToLower().Contains(name.ToLower()))
        {
            exists = false;
        }
    }

    if (!String.IsNullOrEmpty(referenceNumber))
    {
        if (!this.charityReference.ToLower().Contains(referenceNumber.ToLower()))
        {
            exists = false;
        }
    }

    return exists;
}

더 자세한 정보가 필요하면 알려주십시오.

많은 감사합니다

Annelie


알다시피 Entity Framework는 실제로 쿼리의 일부로 C # 코드를 실행할 수 없습니다. 쿼리를 실제 SQL 문으로 변환 할 수 있어야합니다. 이것이 작동하려면 쿼리 표현식을 Entity Framework가 처리 할 수있는 표현식으로 재구성해야합니다.

public System.Linq.Expressions.Expression<Func<Charity, bool>> IsSatisfied()
{
    string name = this.charityName;
    string referenceNumber = this.referenceNumber;
    return p => 
        (string.IsNullOrEmpty(name) || 
            p.registeredName.ToLower().Contains(name.ToLower()) ||
            p.alias.ToLower().Contains(name.ToLower()) ||
            p.charityId.ToLower().Contains(name.ToLower())) &&
        (string.IsNullOrEmpty(referenceNumber) ||
            p.charityReference.ToLower().Contains(referenceNumber.ToLower()));
}

이 코드에서 같은 오류가 발생했습니다.

 var articulos_en_almacen = xx.IV00102.Where(iv => alm_x_suc.Exists(axs => axs.almacen == iv.LOCNCODE.Trim())).Select(iv => iv.ITEMNMBR.Trim()).ToList();

이것은 정확히 오류였습니다.

System.NotSupportedException : 'LINQ to Entities는'Boolean Exists (System.Predicate`1 [conector_gp.Models.almacenes_por_sucursal]) 메소드를 인식하지 못하므로이 메소드를 상점 표현식으로 변환 할 수 없습니다. '

나는 이런 식으로 해결했다.

var articulos_en_almacen = xx.IV00102.ToList().Where(iv => alm_x_suc.Exists(axs => axs.almacen == iv.LOCNCODE.Trim())).Select(iv => iv.ITEMNMBR.Trim()).ToList();

I added a .ToList() before my table, this decouple the Entity and linq code, and avoid my next linq expression be translated

NOTE: this solution isn't optimal, because avoid entity filtering, and simply loads all table into memory


I had a similar problem to yours and this LINQ documentation helped me find the right string functions to work around the limitations.

참고URL : https://stackoverflow.com/questions/7259567/linq-to-entities-does-not-recognize-the-method

반응형