Reputation: 73
May anybody tell me how to replace this code using Linq ?
using using Microsoft.Practices.EnterpriseLibrary.Data;
Public IDataReader GetRowByRowData()
{
Database Db = DatabaseFactory.CreateDatabase();
string sqlString = "SELECT * FROM TableTest";
DbCommand DbCmd = PpwDb.GetSqlStringCommand(sqlString);
Db .ExecuteReader(DbCmd);
}
Please help to get row by row data from table TableTest using Linq
Upvotes: 0
Views: 1189
Reputation: 2892
I load the primary key from my table into a list. Depending on the size of the data set and the primary key, loading into a list does not take too long. After loading the keys, use FirstOrDefault() with a where clause like so:
var keys = Db.TableTest.Select(x => x.primaryKey).ToList();
foreach (var k in keys)
{
var record = (from i in Db.TableTest
where i.primaryKey == k
select new
{
//Select only the columns you need to conserve memory
col1 = i.col1,
col2 = i.col2
}).FirstOrDefault();
//Process the record
}
Upvotes: 0
Reputation: 1619
var records = (from p in context.TableTest
select p).ToList();
foreach(var record in records) {
// loop through each record here
}
ToList
method will query the database and get the result set.
Upvotes: 0
Reputation: 13447
you can do that like this:
var myQyery=from a in dataContext.Mytable
select a;
foreach(var item in myQuery)
{
//what you like
}
Upvotes: 1