p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 (http://p2p.wrox.com/forumdisplay.php?f=264)
-   -   LINQ to SQL example for TheBeerHouse in Wrox Blox (http://p2p.wrox.com/showthread.php?t=66807)

jminatel March 21st, 2008 09:27 AM

LINQ to SQL example for TheBeerHouse in Wrox Blox
 
Doug Parsons, who many of you will know from these forums for all of his fantastic answers to many ASP.NET questions has written for Wrox a short example Migrating to LINQ to SQL in TheBeerHouse and ASP.NET 2.0 Website Programming Problem Design Solution to help anyone who wants to get started with a LINQ to SQL upgrade for their TheBeerHouse site get going.

Here's Doug's description of the content, and there's a full table of contents for the 34 pages Wrox Blox on the page for it:
LINQ to SQL has quickly become a hot topic with the release of ASP.NET 3.5, so it makes sense that the TheBeerHouse application gets an upgraded Data Access Layer leveraging this new technology!
This Wrox Blox demonstrates various LINQ to SQL techniques to use in upgrading the “Articles” portion of TheBeerHouse application. The first step is migrating TheBeerHouse project from an ASP.NET 2.0 to an ASP.NET 3.5 application. Next, create a LINQ to SQL class (DBML) mapping file that will encapsulate the entire Data Access Layer for this project. Readers will become comfortable working with Entities (Tables), navigating Associations (Relationships), and working with stored procedures from a LINQ standpoint, throughout the implementation.

While TheBeerHouse has been ported to Visual Basic .NET, the author uses C# throughout this Wrox Blox, so readers should have at least an intermediate level of understanding with C#.

Code: The code is now available at the usual location at:
http://www.wrox.com/WileyCDA/WroxTit...-DOWNLOAD.html

Jim Minatel
Acquisitions Director
Wiley Technology Publishing
WROX Press
Blog: http://wroxblog.typepad.com/
Wrox online library: http://wrox.books24x7.com

jimibt March 21st, 2008 10:05 AM

this sounds great. am just going to buy the download and have a look thro' it and will probably look to creating a set of templates for the site generator based on the concepts.

nice to see doug having a 1st crack at this.

[edit] it would be 'nice' to include a download of the code from the same page on the account that the pdf came from just to see it in action. any chance of doing this??
jimi

http://www.originaltalent.com

dparsons March 21st, 2008 10:11 AM

Hi Jimi,

Enjoy the article! I also would be very interested to know what you (and the rest of the community for that matter) think of it.

-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========

jimibt March 21st, 2008 10:23 AM

doug - so far so good (tho' haven't opened vs08 yet to try the concepts. as i mentioned above, it would be a great idea to supply the code that you've worked on to get a feel for the layout etc..)

will keep you posted, tho' am off on holiday for 3 weeks (from sunday), so might not be quite as quick to respond to the article as i'd usually be...

[edit] it would be useful also to perhaps see a little bit of linq that demonstrates the use of paged recordsets along the lines of the following (calssic sql):

string sqlQuery = "SELECT * FROM (" + string.Format("SELECT TOP {1} *, ROW_NUMBER() OVER (ORDER BY {0}) AS RowNum
FROM FileUploads", sortExpression != "" ? sortExpression : "AddedBy ASC", upperBound) + ")
AS AllFileUploads WHERE RowNum BETWEEN @Start AND @End ORDER BY RowNum ASC";

just a thought for us lazy types :-)

jimi

http://www.originaltalent.com

dparsons March 21st, 2008 10:40 AM

3 Weeks of vaction, must be nice! Anywho. I shot Jim an email about getting a link to the source code posted so I will let you know when that happens.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========

jimibt March 21st, 2008 10:46 AM

doug - thanks for that, much appreciated. as for the 3 weeks vacation, sounds nice from a distance until you remember that there will be 4 kids in tow!! :)

jimi

http://www.originaltalent.com

dparsons March 21st, 2008 10:52 AM

Paging is quite simple using LINQ, a basic method might be something like this:

private void BindDataSource(int rowNumber)
{
TBHDataContext ctx = new TBHDataContext();
var articles = from a in ctx.Articles
                 where a.ArticleID >= 1;
gv.DataSource = articles.Skip(rowNumber).Take(15);
gv.DataBind();
}

The Skip() and Take() methods is where the magic happens. Take(15) returns the first 15 records starting from the Row position defined in Skip(). So if rowNumber = 1 then Take(15) would return the first 15 records in your result set.

hth.

[edit]: The obvious question might be: "Thats great but how do I know what to skip by?" In the case of the GridView if you handle the PageIndexChanging event you could do something like this:

protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
BindDataSource(GetNextPage(e.NewPageIndex , gv.PageSize));
}

private int GetNextPage(int pageIndex, int pageSize)
{
if(pageIndex == 1){ return 1; }
else
{
     return ((pageIndex * pageSize) - (pageSize - 1));
}
}

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========

jimibt March 21st, 2008 11:06 AM

Quote:

quote:Originally posted by dparsons
 Paging is quite simple using LINQ, a basic method might be something like this:

private void BindDataSource(int rowNumber)
{
TBHDataContext ctx = new TBHDataContext();
var articles = from a in ctx.Articles
                 where a.ArticleID >= 1;
gv.DataSource = articles.Skip(rowNumber).Take(15);
gv.DataBind();
}

The Skip() and Take() methods is where the magic happens. Take(15) returns the first 15 records starting from the Row position defined in Skip(). So if rowNumber = 1 then Take(15) would return the first 15 records in your result set.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
doug, i presume there's a way to do sorting on the linq object as well?? (i.e. if clicking on column headers etc..)

jimi

http://www.originaltalent.com

dparsons March 21st, 2008 11:13 AM

Yes that is possible though you would probably want to use a LinqDataSource to achieve this (as a bonus you could also set your gridview up to use paging, editing, etc with this control)

http://msdn2.microsoft.com/en-us/lib...atasource.aspx

-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========

jimibt March 21st, 2008 02:42 PM

doug - any chance of seeing a version 1.01 of the pdf with this stuff (LinqDataSource, pagin, editting etc) included just to see how all the component pieces fit together (that way all of the beerhouse functionailty using linq would be showcased).

fingers xx'd - :-0

jimi

http://www.originaltalent.com


All times are GMT -4. The time now is 10:08 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.