2014-06-12 39 views

回答

3

您很可能會使用SingleOrDefault或可能Find。這些方法通過選擇最上面的兩行來工作。然後確保只返回一行。如果返回多個元素,則會拋出異常。

如果有多於一個匹配,它還會怎麼知道?

// If there were two Products with a ProductName of Widget then the code would throw an exception. 
context.Products.SingleOrDefault(i => i.ProductName == "Widget"); 

// If there were two Products with a ProductId of 1234 then the code would throw an exception. 
context.Products.Find(1234); 
+0

感謝沒有意識到我們使用的是,我們通常使用FirstOrDefault,但我們的權利,將進一步調查爲什麼。謝謝。並感謝所有其他人爲此付出的努力。 –