I have the following code. It works great.
In the place where I checked, I would like to write a query (suppose LINQ ) that extracts CompanyName where MainKey == 3028
I suspect this is trivial, but I'm new to LINQ , and I was looking for some basic LINQ information on MSDN and cannot make it 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; } } }
source share