Linq to SQL for WebMatrix (Web Pages) - c#

I'm wondering if I can use Linq to SQL in my webmatrix-based sites (C#/Razor etc). I love database searching and stuff and it would be awesome if I could use Linq to SQL for that - or at the least, have rows returned, as a dataset or something and then linq over that. Is this possible with WebMatrix-based sites?
I haven't seen anything about Linq in the docs for WebMatrix.

You can use Linq To SQL or the Entity Framework with Web Pages sites. I would recommend using Visual Studio 2010 SP1 or Visual Web Developer 2010 SP1 as they provide the tooling support for Linq To SQL and Entity Framework. I've written a couple of articles on Entity Framework with Web Pages:
http://www.mikesdotnetting.com/Article/185/Building-A-Razor-Web-Pages-Site-With-Database-First-And-Entity-Framework
http://www.mikesdotnetting.com/Article/182/Entity-Framework-Code-First-Development-With-WebMatrix
Entity Framework is the ORM tool from Microsoft that's receiving all the development attention. While LINQ to SQL is not exactly "dead", it doesn't get as much love. Of the two, EF is the way to go, in my opinion.

Webmatrix provides it's own integrated data tools, these tools are not Linq based.
Nothing prevents you from using Linq as far as I know, but you have to eschew the built-in tools to do so. You won't find a data designer, for instance, that works with L2S or EF. You could use Code first though.

It's easy just add linq in the top:
#using System.Linq;
And after that your query:
var selectedDates = db.Query("Select CompleteDate, DueDate FROM Records WHERE Id=#0", ID);
var Date = selectedDates.Select(s => s.CompleteDate).ToArray();
And that its all =D enjoy

Related

View EF4 generated queries?

LINQ-to-SQL had several ways, including a visualizer add-in, to view the generated SQL from an IQueryable.
I can't find the equivalent for Entity Framework 4. Nothing on StackOverflow, no blogs. How is it done?
Preferably, I'd like to be able to do it in code and without having to actually execute the query just to see it.
Thanks!
there are several approaches to looking at the sql.
Free
On the ObjectQuery do .ToTraceString() that will show u the sql generated for the query.
Download ef tracing provider written by one of the EF team members. EF Tracing Provider
Linq To Entities visualizer which you can download here.
LinqPad
Sqlserver profiler
Commercial
Efprof.com
If you can't get any of the other solutions to work, you could try using the SQL Server Profiler if you have access to the SQL Machine.
Within SQL Server Management Studio you can do the following:
Tools -> SQL Server Profiler.
Create a new Trace and run your code and you should see the queries come across. You can create some filters so you don't see the security / audit stuff which you probably don't care about.
Hi there is this visualizer...but I could not get it to work for me...you could try it..I would recommend LINQPad for viewing your queries you can setup your ef connection and execute your queries.

Which framework and database should i use?

I am looking at creating an application to record gym workout(set,reps, etc) and I was wondering what framework and database backend to use. I am currently thinking C# .Net 3.5 for the framework because I am familiar with it but I'm unsure about how to store the data. Originally I was thinking of xml files and parsing through them but that seems like more work then is needed. If I was to use SQL would I be able to run that from my own machine (Windows 7) and what would be the best method to connect? ODBC, LINQ etc.
Thanks in advance
Linq-to-SQL is a very easy to learn, easy to use and straightforward way to map database tables 1:1 onto domain objects in C#. You can install the free SQL Server Express editions on your machine locally, and use Linq-to-SQL against those.
It has a great set of features, visual designers, and using LINQ queries against SQL Server tables is really quite powerful to use and nice to write.
So unless you have any specific requirements (like supporting backends other than SQL Server or such), I would definitely recommend going with Linq-to-SQL first.
Tutorials:
Scott Guthrie has an outstanding blog post series on Linq-to-SQL and how to use it - highly recommended
The NerdDinner ASP.NET MVC demo app also contains Linq-to-SQL and you can learn a lot from it - great 100+ page intro tutorial as PDF or in HTML format
a bit more advanced: Hooked On LINQ has tons of articles, demos, how-to's and so forth - for LINQ in general and Linq-to-SQL specifically
I would use SQL Server 2008 R2 Express for the backend. It's free and powerful enough for most smaller apps.
Not quite there yet, but this looks like a great place for SQL CE 4.0. Check out ScottGu's post about it: http://weblogs.asp.net/scottgu/archive/2010/06/30/new-embedded-database-support-with-asp-net.aspx
Since it seems to be a small utility kind of application and the database wouldn't be very large I'd suggest to use the SqlCE database. It makes sense to have the database embedded in your app rather than have a server based database. That way you could easily just copy/share your app without having to bother about the database setup.
If you're willing and able to get away from Microsoft solutions. I would highly suggest taking a look at the Ruby on Rails framework as well. It's great for getting things up and running quickly.

Nhibernate HQL SQL Server 2005 / 2008 CONTAINSTABLE

do you know anyway how I to use CONTAINSTABLEvia HQL? I need to use CONTAINSTABLE, not just CONTAINS. Currently I am using a SQL-query, but I need to use the multi-query feature which is not avaiable for SQL right now.
If there is no such thing, would I be able to extend the MSSQL dialect? Or would that require to modify the core of NH?
Thanks in advance!
I have posted a NHibernate patch that will add support for custom SQL queries in a MultiQuery (see NH-2495 on http://jira.nhforge.org).

Does Linq to Entities 4.0 have Fulltext capabilities?

Just wanted to know if Linq to entities 4.0 have fulltext search capabilities? Thanks
There are no specific operators or support for full text directly in EF v4.0.
I think that is a server option.
Anyways, if it does not (in EF), nothing stops you from calling the required SQL directly. :)
I've been using custom stored procedures for my Sql Server 2008 FullText capabilities (eg. CONTAINS(.. .. ). Of course, EF can call stored procedures.
This is an option you can leverage off. Works great for L2S and/or EF.
HTH.

Can you use LINQ tools such as SQLMetal with an access database?

I'm creating a small database application to teach myself the following concepts
C# programming
.Net 3.5 framework
WPF
LINQ ORM
I want to use Microsoft Access as the database but I can't seem to find any mention of whether its possible to use SQLMetal to generate the ORM code from a Microsoft Access database.
Does anyone know if this is possible?
If not, are there any small database or embedded databases I could use? I think SQL express would be overkill for me at this point.
For an embedded database, you can use SQL Server Compact Edition. Unlike SQL Server Express, it is not compatible with the LINQ to SQL designer, but it is fully compatible with the command-line SQLMetal. It has a few advantages over SQL Express, like to ability to use embedded or run from a file. Microsoft has a very handy chart outlining the differences between Express and Compact.
I don't think SQL Express would be overkill if you want to learn real-world skills - quite the opposite in fact! That'd be my choice, and whatever I chose, I'd stay clear of Access.
Good luck
AFAIK, Linq to SQL is MSSQL server provider specific. To be honest, SQL Express is pretty lightweight on todays machines.
BTW don't confuse LINQ with Linq to SQL. Linq is the underlying technology to provide "query" like support to .NET (amongst other things), where as L2S is effectively a Data Access technology built on top of Linq. Vanilla Linq will work with any ADO.NET provider, which of course Access is one.
Entity Framework will work with any compatible provider also but if SQLExpress is too heavy for you then I wouldn't recommend going down this path...
Thanks for all the responses. I never expected to get an answer this quick. For my test application I think SQL Server Compact Edition would be the way to go. I'm basically creating a money managment app similar to Microsoft Money and although it is an exercise to learn skills, I would eventually want to use it to manage my finances (provided its not too crap!)
This why I thought a fully blown database would be overkill.

Categories