views:

4951

answers:

3

I have a ASP.Net page using ADO to query MS access database and as a learning exercise i would like to incorporate LINQ. I have one simple table called Quotes.

The fields are: QuoteID, QuoteDescription, QuoteAuthor, QuoteDate. I would like to run simple queries like, "Give me all quotes after 1995".

How would i incorporate LINQ into this ASP.Net site (C#)

Basically, my question is does LINQ work for MS Access ??

+3  A: 

I don't think LINQ to SQL supports Access. However, if your table is sufficiently small to fit into memory, LINQ to DataSet will let you query datatables etc pretty easily - especially strongly typed datasets.

Jon Skeet
can you explain what you mean by LINQ to DataSet. right now in my query i query to a datatable
ooo
LINQ to DataSet is basically a set of extension methods on DataSet and DataTable. The idea is to be able to perform queries against data you've already fetched from the database. See http://www.danielmoth.com/Blog/2007/07/linq-to-dataset.html for more information.
Jon Skeet
Y'all don't mean "Access" -- you mean JET.
David-W-Fenton
+13  A: 

LINQ to SQL doesn't support Access (that is, there's no Access/Jet provider for LINQ), but you can query a DataSet with LINQ. This means that you fill your DataSet with any possible data from your database that you might need in your results, and then you filter on the client side. After you have a typed DataSet, and you Fill() it with a TableAdapter, you do something like this:

var year = 1995;  // you can pass the year into a method so you can filter on any year
var results = from row in dsQuotes
              where row.QuoteDate > year
              select row;

You'll have to decide whether this is worth it. You'd have to fill your DataSet with all the quotes, then use LINQ to filter on just those quotes that are after 1995. For a small amount of data, sure, why not? But for a very large amount of data, you'll need to make sure it won't be too slow.

If you're using a DataSet, though, you can write custom queries that become new TableAdapter methods. So you can put the correct SQL for your query in a FillByYear() method in your TableAdapter and use that to fill your typed DataTable. That way you're only getting back the data you need.

If you go this route, remember that Access/Jet uses positional parameters, not named parameters. So instead of

SELECT * FROM Quotes WHERE Year(QuoteDate) > @Year

you'd use something like this:

SELECT * FROM Quotes WHERE Year(QuoteDate) > ?
Kyralessa
+1  A: 

Try a third part library -- ALinq,http://www.alinq.org

ansi
Linq entity designer still doesn't work - how to select ALinq as data source provider?
rotary_engine
ALinq website has bad English, and lots of typos. Just saying.
Adam Nofsinger