Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7518157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:40:33+00:00 2026-05-30T01:40:33+00:00

Recently started with linq to sql and i am not sure what is the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:40:34+00:00Added an answer on May 30, 2026 at 1:40 am

    You can also do it like this:

    using(DataContext db = new DataContext(ConfigurationManager.AppSettings["myConnection"]))
    {
        var rate=db.GetTable<RatesClass>()
                 .Where(a=>a.From == "something")
                 .Select(a=>Convert.ToDouble(a.Rate))
                 .SingleOrDefault();
    }
    

    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.

    Tables in a relational database are represented as Table collections
    (which implements interfaces such as IQueryable and IEnumerable).
    DataContext has a method called GetTable<>(); it represents a
    potential interaction with the table of view. The query is not
    actually executed until iteration over the result is performed. The
    type parameter of GetTable() identifies the table in the database
    .

    Reference here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently started to work with Linq to SQL and wondered how to get
I am fairly new to .NET C# development and recently started using LINQ to
Recently i am started working on ASP.NET MVC ( along with Linq to SQL).I
I recently started using LINQ to SQL in my database later for a C#
I started using LINQ to SQL recently. On my DMBL designer, I open the
I am quite new to the Entity Framework, and only have recently started looking
I recently started to use LINQ to SQL and i have a minor complex
I recently started to learn about LINQ and came across the OrderBy extension method.
I recently started a new webforms project and decided to separate the business classes
I've recently started using LINQ quite a bit, and I haven't really seen any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.