Monday, May 12, 2008

LINQ to SQL in Next Beta

First lets select all Employees who have the last name "Griffin" and order them as well. The Employees class highlighted in blue above is your true EntitySpaces class.

DataContext context = new DataContext("User ID=sa;Initial Catalog=Northwind;Data Source=localhost;");

var q = context.GetTable<Employees>().Where(s => s.LastName == "Griffin")
.OrderBy(s => s.LastName);

foreach (Employees emp in q)
{
Console.WriteLine(emp.LastName);
}

Now let's do a join using a different syntax style. Notice that we select the EmployeeID from the Empoyees table and the OrderID from the Orders table.

DataContext context = new DataContext("User ID=sa;Initial Catalog=Northwind;Data Source=localhost;");

var employees = context.GetTable<Employees>();
var orders = context.GetTable<Orders>();

var query = from e in employees
join o in orders on e.EmployeeID equals o.EmployeeID
where e.LastName != "Griffin"
select new { e.EmployeeID, o.OrderID };

foreach (var obj in query)
{
Console.WriteLine(obj .EmployeeID.ToString() + ":" + obj .OrderID.ToString());
}

To add LINQ to SQL support to your EntitySpaces classes all you need to do is check a checkbox on the advanced tab.

When you check this checkbox the templates will make sure our LINQ to SQL support is injected in your classes. The code ends up looking like this.

[Serializable]
[Table(Name = "Employees")]
public partial class Employees : esEmployees
{
#region LINQtoSQL overrides

[Column(IsPrimaryKey = true, CanBeNull = false)]
public override System.Int32? EmployeeID
{
get { return base.EmployeeID; }
set { base.EmployeeID = value; }
}

[Column(IsPrimaryKey = false, CanBeNull = false)]
public override System.String LastName
{
get { return base.LastName; }
set { base.LastName = value; }
}

[Column(IsPrimaryKey = false, CanBeNull = false)]
public override System.String FirstName
{
get { return base.FirstName; }
set { base.FirstName = value; }
}

[Column(IsPrimaryKey = false, CanBeNull = true)]
public override System.String Title
{
get { return base.Title; }
set { base.Title = value; }
}

// and so on ....

#endregion
}

Of course you will need to be targeting .NET 3.5 and the generated classes will include these additional namespaces:

using System.Data.Linq;
using System.Data.Linq.Mapping;

This will work for any .NET Providers that support LINQ to SQL, currently I believe that is only Microsoft's SQL Client (but I could be wrong, I know the Npgsql guys are working on this too). So, now you can have all of the wonderful EntitySpaces features, including our Dynamic API (which runs on all of our supported databases and .NET 2.0) and LINQ as well if you so choose.

From mobile devices to large scale enterprise solutions in need of serious transaction support, EntitySpaces can meet your needs. Whether you’re writing an ASP.NET application with medium trust requirements, a Mono application, or a Windows.Forms application, the EntitySpaces architecture is there for you. EntitySpaces is provider independent, which means that you can run the same binary code against any of the supported databases. EntitySpaces is available in both C# and VB.NET. EntitySpaces uses no reflection, no XML files, and sports a tiny foot print of less than 200k. Pound for pound, EntitySpaces is one tough, dependable .NET architecture.

No comments:

.

.