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

9
  • 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. Commented Dec 11, 2014 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 Commented Dec 11, 2014 at 15:35
  • LINQ 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. Commented Dec 11, 2014 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. Commented Dec 11, 2014 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. Commented Dec 11, 2014 at 16:01

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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); } } 

1 Comment

Why are you performing a pointless projection of the data? Just omit it if you have no operation to perform.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.