Recently started with linq to sql and i am not sure what is the most efficient way to query a db and only get what i need.
DataContext db = new
DataContext(ConfigurationManager.AppSettings["myConnection"]);
Table<RatesClass> CurrencyRatestbl = db.GetTable<RatesClass>();
double Rate = 0.00;
Rate =
(from c in CurrencyRatestbl
where c.From == "something"
select Convert.ToDouble(c.Rate)).Single();
I think db.GetTable get all the records from the table but i want only get one record from db, is there a way to do it.
Note: the linq query will always get one record “something” is the product name, so for every product name there will be a single rate.
You can also do it like this:
I also think that it is best practice to have the database context inside a using statement. So that the connection to the database is open as long as it needs. The get table do not get all the records before is it actually executed.
Reference here