1

Suppose I have a Table in my database named Table1. I have 3 columns in Table1 named

FirstName
SurName
DOB

In sql I would simply do select * from Table1 and it'll display everything from that particular table. However what I am trying to understand is how would I select all the values from this table using Linq in C#. Table1 is in the database and the front-end is being developed using ASP.NET and C# I just can't seem to get my head around this. My knowledge on linq is very little so do excuse me if I'm making an obvious mistake

Izzy
  • 6,163
  • 5
  • 32
  • 73
  • LINQ is just a query language. It isn't specific to databases. I would suggest you take a look at an ORM like Entity Framework since it can pick up a lot of the work for you. You may also want to take databases out of the question for now, just practice using LINQ on a basic in-memory array of something. – Arran Dec 11 '14 at 15:30
  • @Arran The website I am currently working on uses EF 6 I have been given the project half way through so need to get used to really quickly. I appreciate your suggestions – Izzy Dec 11 '14 at 15:35
  • [LINQ](http://msdn.microsoft.com/en-us/library/bb397926.aspx) means Language Integrated Query - it is used to query something integrated in the language. For example, you could query a `List<>` by using `.Where()`. It has not much to do with databases actually. – Dion V. Dec 11 '14 at 15:36
  • @DionV. It has *something* to do with databases, although not *everything* to do with databases. Half of LINQ is the ability to query external resources (such as databases) from a querying language that's integrated into the language. Only half of LINQ is for querying in-memory sequences of data. – Servy Dec 11 '14 at 15:38
  • @Servy just to make my point and because I'm stubborn; no. LINQ *itself* has *nothing* to do with databases. *Extensions* such as LINQ to SQL, do. The OP does not specifically ask for either of them, meaning I gave him the definition of the most simple one. – Dion V. Dec 11 '14 at 16:01
  • @DionV. LINQ is a tool that can be used, among other things, to create queries that can be executed against a database. That's not *nothing* to do with databases. Creating a tool for writing database queries certainly has *something* to do with querying databases. You would have been right had you said that LINQ doesn't *execute* database queries; it just builds them. – Servy Dec 11 '14 at 16:08
  • @Servy *" While LINQ is primarily implemented as a library for .NET Framework 3.5, it also defines optional language **extensions** that make queries a first-class language construct and provide **syntactic sugar** for writing queries."*. From [here](http://en.m.wikipedia.org/wiki/Language_Integrated_Query). – Dion V. Dec 11 '14 at 16:54
  • @DionV. Okay. And? How does a language extension that allows *database queries* to be defined using an query syntax that's built into the language proving your point, rather than mine? – Servy Dec 11 '14 at 17:02
  • @Servy Gosh; *"provide **syntactic sugar** for writing queries"*. LINQ does not always produce database queries. Extensions of LINQ produce database queries. If you look up the standard `Where()` for example, it returns an `IEnumerable`, whereas the EF6 extension, for example, returns an `IQueryable`. **There is a difference**. – Dion V. Dec 11 '14 at 17:13

3 Answers3

1

See below links for an intro to linq

What is Linq and what does it do?

http://weblogs.asp.net/scottgu/using-linq-to-sql-part-1

Linq provides a mean of querying data, but you still need to provide a means of Linq accessing that data - be it through Linq2Sql classes, ADO, Entity Framework, etc.

I'm a fan of Entity Framework (EF) where you set up objects that represent your data, and use a context to populate those objects.

it could look something like this:

public class Table1
{
    public string FirstName { get; set; }
    public string SurName { get; set; }
    public DateTime DOB { get; set; }
}

public class Table1Repository
{
    private readonly MyEntities _context;

    public Table1Repository()
    {
        this._context = new MyEntities();
    }

    public IQueryable<Table1> Get()
    {
        return this._context.Table1; // in effect your "Select * from table1"
    }

    public IQueryable<Table1> GetById(DateTime dob)
    {
        return this._context.Table1.Where(w => w.DOB == dob); // pulls records with a dob matching param - using lambda here but there is also "query expression syntax" which looks more like sql
    }

}

Note that you're performing linq queries on the context that represents the data, not the database itself. Linq is very powerful, but you need to provide it a means of accessing data. Even if that data is as xml, a file, a database, whatever!

Community
  • 1
  • 1
Kritner
  • 12,693
  • 10
  • 45
  • 68
1

In Linq2Sql you would select all field quite simply by

Starting with a datacontext:

var db = new YourDataContext()

And after that you can do things like

var myData = from row
             in db.table1
             select row

As you indicate yourself, your knowledge is too limited. Check out this series about L2S: http://weblogs.asp.net/scottgu/using-linq-to-sql-part-1

Pleun
  • 8,836
  • 1
  • 28
  • 46
0

Since you are using EF6 you can read your table using LinqToEntity.

public ObservableCollection<Table1> ReadTable1()
{            
    using (YourDBContext dc = new YourDBContext())
    {
        var data = (from x in dc.Table1 
                    select x);

        return new ObservableCollection<Table1>(data);
    }            
}
Luca Perotti
  • 196
  • 1
  • 8
  • Why are you performing a pointless projection of the data? Just omit it if you have no operation to perform. – Servy Dec 11 '14 at 16:08