【发布时间】: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