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; }
}
}