1

I am trying to retrieve a single row from my table using Entity Framework core. This seems to be very simple, but i cannot get it to work.

I am able to get the list by using

_context.COUNTRY.ToList();

This gives me the full list of countries in my table. Now I need to get only one country that matches the ID. How to do this as Find() is missing in the EF core. Can we do a SELECT with WHERE. Can someone help me with the syntax.

My COUNTRY model is as shown below

public partial class COUNTRY { public string COUNTRYID { get; set; } public string COUNTRYNAME { get; set; } }

1 Answer 1

1

This worked

_context.COUNTRY.Single(c => c.COUNTRYID == id);

Sign up to request clarification or add additional context in comments.

1 Comment

You can also do SingleOrDefault incase you don't want it to break. This answer works fine but i does "select * from table". If you want only fwe columns please do _context.COUNTRY.Where(c => c.COUNTRYID == id).Select(f=>{f.CountryID}).SingleOrDefualt();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.