My First LINQ Application in Orcas
8/9/2007 7:03:55 PM
Hello Orcas!
Very much testing the waters. This took me all of 10 minutes to put together. Man, I can't wait for someone to write a book on .NET 3.5 Generics. I can't wait. To think I just finished .NET 2.0 Generics!
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
<title>My First LINQ Page!
</title>
</head>
<
body>
<form id="form1" runat="server">
<
p>
<a href="default.aspx?accountTypeId=1">1
</a> ~
<a href="default.aspx?accountTypeId=2">2
</a> ~
<a href="default.aspx?accountTypeId=3">3
</a> ~
<a href="default.aspx?accountTypeId=4">4
</a>
</p>
<
table border="0" cellpadding="8" cellspacing="8">
<tr>
<td><strong>AccountTypeId
</strong></td>
<td><strong>EmailAddress
</strong></td>
<td><strong>Username
</strong></td>
</tr>
<asp:Repeater ID="repeaterAccountInformation" runat="server">
<ItemTemplate>
<tr>
<td><%# Eval("AccountTypeId") %></td>
<td><%# Eval("EmailAddress") %></td>
<td><%# Eval("Username") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Web.UI;
public
partial class _Default :
Page
{
protected void Page_Load(
object sender,
EventArgs e)
{
if (!
string.IsNullOrEmpty(Request.QueryString[
"accountTypeId"]))
BindResults(
int.Parse(Request.QueryString[
"accountTypeId"]));
}
private void BindResults(
int accountTypeId)
{
repeaterAccountInformation.DataSource =
LinqHelper.
Accounts.GetAccountsByAccountType(accountTypeId);
repeaterAccountInformation.DataBind();
}
}
LinqHelper.cs
using System.Linq;
using System.Configuration;
using System.Collections.Generic;
public abstract
class LinqHelper
{
public abstract class Accounts
{
public static IEnumerable<
Account> GetAccountsByAccountType(
int accountTypeId)
{
SimpleSchedulerDB db =
new SimpleSchedulerDB();
return from account in db.Accounts where account.AccountTypeId == accountTypeId select account;
}
}
}
I won't even bother adding the code that was generated for my Linq to Sql class. I will say that I took it upon myself to refactor the hell out of it. Those that know me well enough will probably agree that I have a mild-to-moderate case of OCD when it comes to code generation. Even though the generated code is not SUPPOSED to be edited I took it upon myself to refactor. I can't continue being productive knowing that there is a generated CS class in my App_Code directory full of this.'s and un-optimized using statements. Call me crazy. You're talking to a guy that adds Tabs and Environment.NewLine's when adding composite controls to a PlaceHolder so I can still have organic-looking HTML.
LINQ,
Orcas
