tags:

views:

173

answers:

4

Hi

How do you select all rows when doing linq to sql?

Select * From TableA

In both query syntax and method syntax please.

Thanks

+3  A: 
from row in TableA select row

Or just:

TableA

In method syntax, with other operators:

TableA.Where(row => row.IsInteresting) // no .Select(), returns the whole row.

Essentially, you already are selecting all columns, the select then transforms that to the columns you care about, so you can even do things like:

from user in Users select user.LastName+", "+user.FirstName
Simon Buchan
A: 

Do you want to select all rows or all columns?

Either way, you don't actually need to do anything.

The DataContext has a property for each table; you can simply use that property to access the entire table.

For example:

var allOrders = context.Orders;

foreach(var order in allOrders) {
    //Do something
}
SLaks
Why was this downvoted?
SLaks
A: 
using (MyDataContext dc = new MyDataContext())
{
    var rows = from myRow in dc.MyTable
               select myRow;
}

OR

using (MyDataContext dc = new MyDataContext())
{
    var rows = dc.MyTable.Select(row => row);
}
Simon Fox
Don't do either of these. Instead, simply write `var rows = dc.MyTable`.
SLaks
he asked for query syntax and method syntax so thats what I am giving him.
Simon Fox
Don't give him what he asked for; give him what he needs. He obviously doesn't understand LINQ-to-SQL very well.
SLaks
Nope I don't understand LINQ-to-SQL very well yet. Don't have the time to read books about it yet just sort of when I need information then I start looking around. Plus I am waiting till the Enity frameworks better. Since I rather learn that since it can use all databass.
chobo2
Fair enough, however there is nothing wrong with either of the solutions I have provided. They give you a good base to start from in terms of building up a query of greater complexity.
Simon Fox
A: 

Dim q = From c In TableA Select c.TableA

ObjectDumper.Write(q)
SattiS
This is VB. The OP's other questions show that he uses C#.
SLaks