这是我的代码。我一直收到这个错误。我是编程和这个网站的新手。希望我能正确发布问题。谢谢
protected void btnSave_Click(object sender, EventArgs e)
{
using(TestEntities myEntity = new TestEntities())
{
Product myProduct;
//if insert
if (_id == -1)
{
myProduct = new Product();
myProduct.CreateDateTime = DateTime.Now;
myProduct.UpdateDateTime = DateTime.Now;
myEntity.AddToProducts(myProduct);
}
//update case
else
{
myProduct = (from p in myEntity.Products
where p.ProductID == _id
**select p).SingleOrDefault;**
myProduct.UpdateDateTime = DateTime.Now;
}
答案 0 :(得分:2)
从错误中查看代码时,您遗漏了()
SingleOrDefault
方法。应该是这样的:
var product = query.SingleOrDefault();
//^^^^ Missing brackets
自从您发布代码后,很明显您使用SingleOrDefault
myProduct = (from p in myEntity.Products
where p.ProductID == _id
select p).SingleOrDefault();
//^^^^ here
由于SingleOrDefault
接受谓词,您可以这样做:
myProduct = myEntity.Products.SingleOrDefault(r=> r.ProductID == _id);