I have searched on the internet for the answer to this question with no luck. I have a model that exists in the database created with entity framework and is accessed via db (an instance of my DBContext class).
Function Details(id As Integer) As ViewResult
Dim bill As Bill = db.Bills.Find(id)
Return View(bill)
End Function
The model for the bill class is defined as:
Public Class Bill
Public Property BillId() As Integer
Public Property CustomerId() As String
Public Property BillStatus() As String
End Class
Assuming then that in the function mentioned first I was passed the CustomerId rather than the primary key of this model (BillId), how would I use this to create a ‘Dim bill As Bill’ that included only bills with that customerId.
As in the example above where Bills was filtered with .Find(id) – which only looks at the primary key – I want to be able to use a similar method to .Find() but on a non key field: CustomerId in this case.
Or if you are sure there will always only be one entity matching that CustomerId you can use Single.
VB .NET
C# .NET
Note, this will throw an exception if exactly 1 is not found.