【发布时间】:2014-01-29 15:37:05
【问题描述】:
我正在使用 VS2012 和 DBContext(实体框架)。
以下查询抛出异常
Customer cust;
using (var context = new MiniNorthwindContext()) {
cust = (from c in context.Customers
where c.CustomerName.Contains("SN")
select c).SingleOrDefault();
}
当我运行它时,它会抛出以下错误:“序列包含多个元素”。
如果我将其替换为
Customer cust;
using (var context = new MiniNorthwindContext()) {
cust = (from c in context.Customers
where c.CustomerName == "SN Software"
select c).SingleOrDefault();
}
然后它通过了。
我可能做错了什么。
【问题讨论】:
-
在这里指定 Visual Studio 的版本是完全多余的。 1. 问题明显和IDE无关 2. IDE的版本几乎没有说明framework的版本
-
总而言之,您有多个包含字符“SN”的记录,如果您只想获取第一个(可能不会),只需使用 FirstOrDefault() 代替
标签: c# linq entity-framework