LINQ - linq to object - Asked By balaji on 19-May-11 03:14 AM

i'm having student table in DataClasses1.dbml..it contain  column id

student
id
1
2
3
now how to write a LINQ query to display the column id values in console.......?

Reena Jain replied to balaji on 19-May-11 03:25 AM
hi,

try this

you can produce a log output to show the SQL commands that LINQ to SQL generates. This logging feature (which uses Log) is helpful in debugging, and in determining that the commands being sent to the database accurately represent your query.
try something like this
 
var a = from record in table.Elements("Record")
    select new
    {
        one = (string)record.Elements().ElementAt(0),
        two = (string)record.Elements().ElementAt(1)
    };

//or
 
// Attach the log to show generated SQL.
db.Log = Console.Out;
 
// Query for id.
IQueryable<student> custQuery =
  from stud in students
       select stud;
foreach (Customer cust in custQuery)
{
  Console.WriteLine("ID={0}", stud.ID,);
}
 
// Prevent console window from closing.
Console.ReadLine();   

hope this will help you
balaji replied to Reena Jain on 19-May-11 03:41 AM

the following code will work or not....?
DataClasses1DataContext
dc = new DataClasses1DataContext();

var a=from i in dc.students select i;

Jitendra Faye replied to balaji on 19-May-11 04:17 AM


For this first you have to create object of DataContext.

The data context provides the mapping of all entities (essentially tables) to the database. It is through the data context that the application can query the database, and it is through the data context that changes to the database can be executed.

follow this example -

public void SimpleQuery()
{
    DataClasses1DataContext dc = new DataClasses1DataContext();
    var q =
        from a in dc.GetTable<Order>()
        select a;
    dataGridView1.DataSource = q;
}