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

  • SEARCH
  • Home
  • 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 8797517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:52:27+00:00 2026-06-13T23:52:27+00:00

I have the following code. It runs fine. In the place I have marked

  • 0

I have the following code. It runs fine.

In the place I have marked I’d like to write a query (I assume with LINQ) which extracts the CompanyName where the MainKey == 3028

I suspect this is trivial but I’m new to LINQ and I’ve looked up some basic LINQ info on MSDN and can’t seem to get it to work.

namespace EntityFrameworkExperiment {
class Program {

    static void Main(string[] args) {
        var models = SelectTop100Models("SELECT top 100 * FROM WH.dbo.vw_DimXXX");
        Console.Write("hello world");

        //<<<<<<<linq query to pull out companyname when MainKey == 3028

        Console.Read();
    }

    static IEnumerable<MyModel> SelectTop100Models(string myCommandText) {
        var connectionString = ConfigurationManager.ConnectionStrings["XXX"].ConnectionString;
        using(var conn = new SqlConnection(connectionString))
        using(var cmd = conn.CreateCommand()) {
            conn.Open();
            cmd.CommandText = myCommandText;
            using(var reader = cmd.ExecuteReader()) {
                while(reader.Read()) {
                    yield return new MyModel {

                        MainKey = reader.GetInt32(reader.GetOrdinal("MainKey")),
                        ServerId  = reader.GetInt32(reader.GetOrdinal("ServerId")),
                        CompanyId = reader.GetInt32(reader.GetOrdinal("CompanyId")),
                        CompanyName = reader.GetString(reader.GetOrdinal("CompanyName")),


                    };
                }
            }
        }
    }
}

public class MyModel {

    public int MainKey { get; set; }
    public int ServerId { get; set; }
    public int CompanyId { get; set; }
    public string CompanyName { get; set; }

}

}
  • 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-06-13T23:52:28+00:00Added an answer on June 13, 2026 at 11:52 pm

    Add using System.Linq

    The query should be

    var companyName = models
      .Where(o => o.MainKey == 3028) // apply the filter
      .Select(o => o.CompanyName)    // tell it you only need the one property
      .FirstOrDefault();             // take the first result it finds or use 'null' if the MainKey does not exist
    

    But there is one thing you have to remember – here you are not using LINQ queries to the SQL server – instead you are retrieving all data in memory and then filtering them in .NET. What this means is that if the database contains millions of rows, they will all be pulled from the SQL server. You are applying TOP 100 but that will get you into trouble if the key 3028 is not within the first 100.

    What you should be doing is creating a model using Entity Framework (or a similar tool) and then writing a query that target the classes generated by it. The good thing though is that the LINQ query will be exactly the same – it will just be translated to SQL behind the scenes.

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

Sidebar

Related Questions

I have the following code which runs fine on my computer but fails online:
I have the following code which assembles and runs fine on Windows XP 32
I have the following code which compiles and runs fine under Visual Studio 2008
I have the following jQuery code which runs when I'm clicking an option in
I have the following code: var actionsToExecute = _messagesToExecute.Where(m => m.CanExecute).ToList(); It runs fine
I have the following code which works FANTASTIC. Except - It is a sub-query
Does anyone have a good technical reason why the following code runs fine in
I have the following SQL code that runs against a Change Request database. Each
I have the following code, which shows a squiggly red line under the lambda
I have a stored procedure running in MS SQL 2008 R2 which runs fine

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.