如何在 linq 中选择所有(*)到 sql

在执行 linq to sql 时如何选择所有行?

Select * From TableA

请同时使用查询语法和方法语法。

293727 次浏览
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

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:

foreach(var line in context.Orders) {
//Do something
}
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);
}
Dim q = From c In TableA
Select c.TableA


ObjectDumper.Write(q)

Why don't you use

DbTestDataContext obj = new DbTestDataContext();
var q =from a in obj.GetTable<TableName>() select a;

This is simple.

u want select all data from database then u can try this:-

dbclassDataContext dc= new dbclassDataContext()
List<tableName> ObjectName= dc.tableName.ToList();

otherwise You can try this:-

var Registration = from reg in dcdc.GetTable<registration>() select reg;

and method Syntex :-

 var Registration = dc.registration.Select(reg => reg);

You can use simple linq query as follow to select all records from sql table

var qry = ent.tableName.Select(x => x).ToList();

Assuming TableA as an entity of table TableA, and TableADBEntities as DB Entity class,

  1. LINQ Method
IQueryable<TableA> result;
using (var context = new TableADBEntities())
{
result = context.TableA.Select(s => s);
}
  1. LINQ-to-SQL Query
IQueryable<TableA> result;
using (var context = new TableADBEntities())
{
var qry = from s in context.TableA
select s;
result = qry.Select(s => s);
}

Native SQL can also be used as:

  1. Native SQL
IList<TableA> resultList;
using (var context = new TableADBEntities())
{
resultList = context.TableA.SqlQuery("Select * from dbo.TableA").ToList();
}

Note: dbo is a default schema owner in SQL Server. One can construct a SQL SELECT query as per the database in the context.

I often need to retrieve 'all' columns, except a few. so Select(x => x) does not work for me.

LINQPad's editor can auto-expand * to all columns.

enter image description here

after select '* all', LINQPad expands *, then I can remove not-needed columns.

enter image description here

Simple -

var data=db.table.ToList();

data is variable

db is your dbcontext variable

table is dbset Table from which data you want to fetch.

And finally converting in list.