简体   繁体   English

方法'{0}'不支持SQL转换

[英]Method '{0}' has no supported translation to SQL

I was doing mapping with wildcards. 我正在使用通配符进行映射。 I get this error because the definition of the rule to be searched only comes from a table. 我收到此错误,因为要搜索的规则的定义仅来自表。

I do not know exactly how right I am doing this here, but as a result I have a mistake and I expect your help. 我不完全知道我在这里做的正确程度,但是结果是我犯了一个错误,希望得到您的帮助。

public class Ekstre   
{
    private readonly DataClasses1DataContext db = new DataClasses1DataContext();
    private readonly KdrGnyClassesDataContext kg = new KdrGnyClassesDataContext();

    public bool check { get; set; }
    public int Id { get; set; }
    public DateTime Tarih { get; set; }
    public string Kodu { get; set; }
    public string Açıklama { get; set; }
    public decimal Tutar { get; set; }
    public string bankaKod { get; set; }
    private string kod = null;

    public string muhKod {
        get { return kod = kg.kuralTanimlari
                             .FirstOrDefault(a => Regex.IsMatch(Açıklama, WildCardToRegular(a.kural))).hesapKodu;
            }
        set { kod = value; }
    }

    private string hesap = null;

    public string hesapAdi {
        get { 
            hesap = !string.IsNullOrWhiteSpace(muhKod) ? db.MUHHESAP.First(p => p.MUHKOD == muhKod).MUHADI1 : null;
            return hesap;
        }
        set => hesap = value;
    }

    public string kodTipi { get; set; }
}

public static string WildCardToRegular(string value) 
{
    return "^" + Regex.Escape(value).Replace("\\?", ".").Replace("\\*", ".*") + "$";
}

You can only use supported functions on dbcontext queries over IQuerable. 您只能通过IQuerable在dbcontext查询上使用受支持的函数。 You have two options 1. Fire db query by doing ToList() or ToArray() and then .FirstOrDefault(.....) This has a drawback that it will fetch all records from db and then apply your function to results. 您有两个选择:1.通过执行ToList()或ToArray(),然后执行.FirstOrDefault(.....),触发db查询。这样做的缺点是,它将从db获取所有记录,然后将函数应用于结果。

  1. Convert your filtering function to a simple sql supported function. 将您的过滤功能转换为简单的sql支持的功能。 As far as i know Regex is not supported in entity framework 据我所知,实体框架不支持Regex
public string muhKod {
        get
        {
            var firstOrDefault = kg.kuralTanimlari.ToList()
                .FirstOrDefault(a => Regex.IsMatch(Açıklama, WildCardToRegular(a.kural)));
            if (firstOrDefault != null)
                 kod = firstOrDefault.hesapKodu;
            return kod;
        }
        set => kod = value;
    }

It works so beautifully. 它是如此精美。 Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM