问题
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.
回答1:
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.
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/10626492/linq-to-sql-for-webmatrix-web-pages