How to do Select All(*) in linq to sql

前端 未结 9 1772
傲寒
傲寒 2020-12-13 01:12

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

Select * From TableA

In both query syntax and method syntax please.

9条回答
  •  隐瞒了意图╮
    2020-12-13 01:57

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

    1. LINQ Method
    IQueryable result;
    using (var context = new TableADBEntities())
    {
       result = context.TableA.Select(s => s);
    }
    
    1. LINQ-to-SQL Query
    IQueryable 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 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.

提交回复
热议问题