Simple question. I have entity Customer in my edmx model. I need to get the customer with Id = 20 in c#. How do I do that?
Entities.Customer.First(c => c.CustomerId == 20);
You could also use a LINQ approach, as follows:
Customer customerRecord =
(from customer in Entities.Customers
where customer.id == 20
select customer).FirstOrDefault();
Using FirstOrDefault()
will return null
if the element with that ID doesn't exist, as opposed to First()
which will throw an exception. Also, make sure to include, using System.Linq;
before using LINQ statements.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With