【问题标题】:My first try at converting from LINQ to SQL to LINQ to Entity我第一次尝试从 LINQ 到 SQL 到 LINQ 到 Entity 的转换
【发布时间】:2018-02-25 22:36:33
【问题描述】:

该程序进行了一些非常简单的查找来为支持普通话和英语的抽认卡程序生成数据

我删除了 .dbml 文件并改为创建了 Linq to Entities。

原来的查找是这样的:

using (var ceDictionary = new CeDictDataContext(Properties.Settings.Default.ChineseStudyConnection))
            {
                var definitions = from ed in ceDictionary.CeDicts
                                    where ed.Char == WorkTraditional.Text
                                    where ed.Bopo == foundBo[0]
                                    select ed;

                try
                {
                    foreach (var definition in definitions)
                    {
                        textDefinition.Text = definition.English;
                        break;
                    }
                }
                catch (System.Data.StrongTypingException eng)
                {
                    Status.Text = eng.Message;
                    textDefinition.Text = @"DBNull";
                }
            }

经过一番摸索和浏览文档后,我决定只需要更新 using 语句并将“select ed”更改为“select new”

像这样:

using (var ceDictionary = new ChineseStudyEntities())
            {
                var definitions = from ed in ceDictionary.CeDicts
                    where ed.Char == WorkTraditional.Text
                    where ed.Bopo == foundBo[0]
                    select new
                    {
                        ed.English
                    };

                try
                {
                    foreach (var definition in definitions)
                    {
                        textDefinition.Text = definition.English;
                        break;
                    }
                }
                catch (System.Data.StrongTypingException eng)
                {
                    Status.Text = eng.Message;
                    textDefinition.Text = @"DBNull";
                }
            }

好消息是:编译器很高兴并且项目构建没有错误。

坏消息是:foreach 语句在运行时崩溃并出现此(我无法理解)错误消息:Message=LINQ to Entities 无法识别方法 'System.String get_Item(Int32)' 方法,并且此方法不能翻译成商店表达式。

这是难以理解的,因为我从数据库中提取的一列是一个字符串,我无法开始猜测为什么会以某种方式涉及“get_Item(Int32)”。 ('where' 子句中的两列也是字符串。)

【问题讨论】:

    标签: c# linq-to-sql linq-to-entities


    【解决方案1】:

    我认为您的问题在于这部分:foundBo[0]。按索引访问字母无法转换为普通 SQL。

    尝试改用Substring

    var definitions = from ed in ceDictionary.CeDicts
        where ed.Char == WorkTraditional.Text
        where ed.Bopo == foundBo.Substring(0, 1)
        select ed;
    

    【讨论】:

    • 足够接近,但 'foundBo' 不是字符串,它是一个数组。令我困惑的是 LINK to SQL 能够进行转换,但由于某种原因,LINK to Entities 不能。
    【解决方案2】:

    LINQ to SQL 无法将表达式foundBo[0] 转换为 SQL,将其复制到变量:

    var temp = foundBo[0];
    
    var definitions = from ed in ceDictionary.CeDicts
                      where ed.Char == WorkTraditional.Text
                      where ed.Bopo == temp
                      select ed;
    

    【讨论】:

    • “临时”版本完成了这项工作。惊人!当我还在查看我刚刚提交的问题时,答案就出现了!
    猜你喜欢
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多