43

I want to select all rows from a table using the following type of syntax:

public IQueryable<Company> GetCompanies()
{
    return DbContext.Set<Company>()
    .// Select all
}

Forgive me as I am completely new to EF.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
Wesley Skeen
  • 7,977
  • 13
  • 42
  • 56

3 Answers3

57

Set<T>() is already IQueryable<T> and it returns all rows from table

public IQueryable<Company> GetCompanies()
{
    return DbContext.Set<Company>();    
}

Also generated DbContext will have named properties for each table. Look for DbContext.Companies - it's same as DbContext.Set<Company>()

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
23

The normal way to do this is by instantiating your dbContext.

For example:

public IQueryable<Company> GetCompanies()
{
    using(var context = new MyContext()){ 
        return context.Companies;
    }
}

There are lots of good tutorials on using CodeFirst Entity framework (which i assume you are using if you have a DbContext and are new)

undefined
  • 33,537
  • 22
  • 129
  • 198
10

I prefer work on list, also have all relations here

For example:

public List<Company> GetCompanies()
{
    using (var context = new MyContext())
    {
        return context.Companies.ToList();
    }
}
Artekat
  • 101
  • 1
  • 3